Friday, 13 July 2012

Script for client side validation for text box



<script type="text/javascript" language="javascript">
    function Trim(TrimString) {
        var tempObj;
        if (TrimString) { tempObj = TrimString; TrimString = tempObj.value; }
        if (TrimString == "undefined" || TrimString == null) return null;
        if (TrimString.length == 0) TrimString = "";
        else {
            var Lcont = 0, TempStrArray = TrimString.split(" "), Rcont = TempStrArray.length - 1;
            while (TempStrArray[Lcont] == "") Lcont++; while (TempStrArray[Rcont] == "") Rcont--;
            if (Rcont < Lcont) TrimString = ""; else
                for (TrimString = TempStrArray[Lcont], Lcont++; Lcont <= Rcont; Lcont++) TrimString += " " + TempStrArray[Lcont];
            var re = /\'|/g;
            TrimString = TrimString.replace(re, "");
        }
        if (tempObj && TrimString != "undefined" && TrimString != null) {
            tempObj.value = TrimString; return tempObj
        }
        else return TrimString;
    }
    function fnValidation() {
        
        document.getElementById('<%= lblerror.ClientID  %>').innerHTML = "";
        document.getElementById('<%= lblerror.ClientID  %>').style.color = 'red';
        var rval = true;
        var rvalnew = true;
        var ErrorMsg = "";
        var objFocus;
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        Trim(document.getElementById('<%= txtreenter.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value.trim() != document.getElementById('<%= txtreenter.ClientID  %>').value.trim()) {
            ErrorMsg = "Password mismatches please enter the correct password";
            objFocus = document.getElementById('<%= txtreenter.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtreenter.ClientID  %>'));
        if (document.getElementById('<%= txtreenter.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtreenter.ClientID  %>');
            rval = false;
        }
        var regpassword = /^.*(?=.*\d)(?=.*[@#$%^&+=]).*$/;
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value.match(regpassword) == null) {
            ErrorMsg = "Password should contain atleast one special charcter and number";
            objFocus = document.getElementById('<%= txtpsword.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value.length < 9) {
            ErrorMsg = "Please enter the Password between 9-20";
            objFocus = document.getElementById('<%= txtpsword.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtpsword.ClientID  %>'));
        if (document.getElementById('<%= txtpsword.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtpsword.ClientID  %>');
            rval = false;
        }
        var emailrg = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        Trim(document.getElementById('<%= txtemail.ClientID  %>'));
        if (document.getElementById('<%= txtemail.ClientID  %>').value.match(emailrg) == null) {
            ErrorMsg = "Please enter the correct email id";
            objFocus = document.getElementById('<%= txtemail.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtemail.ClientID  %>'));
        if (document.getElementById('<%= txtemail.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank";
            objFocus = document.getElementById('<%= txtemail.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtphone.ClientID  %>'));
        if (document.getElementById('<%= txtphone.ClientID  %>').value.length < 10) {
            ErrorMsg = "Please enter the phone number between 10-20 characters";
            objFocus = document.getElementById('<%= txtphone.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtphone.ClientID  %>'));
        var regnumberphone = /[^0-9.-]+/;
        if (document.getElementById('<%= txtphone.ClientID  %>').value.match(regnumberphone) != null) {
            ErrorMsg = "Please enter number only";
            objFocus = document.getElementById('<%= txtphone.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtphone.ClientID  %>'));
        if (document.getElementById('<%= txtphone.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtphone.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtexp.ClientID  %>'));
        var regnumber = /[^0-9.-]+/;
        if (document.getElementById('<%= txtexp.ClientID  %>').value.match(regnumber) != null) {
            ErrorMsg = "Please enter only number";
            objFocus = document.getElementById('<%= txtexp.ClientID  %>');
            rval= false;
        }
        Trim(document.getElementById('<%= txtexp.ClientID  %>'));
        if (document.getElementById('<%= txtexp.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank "
            objFocus = document.getElementById('<%= txtexp.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtqul.ClientID  %>'));
        if (document.getElementById('<%= txtqul.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtqul.ClientID  %>');
            rval = false;
        }
        Trim(document.getElementById('<%= txtfst.ClientID  %>'));
        if (document.getElementById('<%= txtfst.ClientID  %>').value == "") {
            ErrorMsg = "Fields marked with (*) cannot be left blank ";
            objFocus = document.getElementById('<%= txtfst.ClientID  %>');
            rval = false;
        }
        if (rval == false) {
            objFocus.focus();
            document.getElementById('<%= lblerror.ClientID  %>').innerHTML = ErrorMsg;
        }
        return rval;


    }
    
</script>


 <asp:Button ID="btnsubmit" runat="server" Text="Submit" CssClass="ms-ButtonHeightWidth"
                OnClientClick="javascript:return fnValidation()" OnClick="btnsubmit_Click" />

No comments:

Post a Comment