November 25, 2016

Javascript validation and useful methods

Perfect Calendar in textbox with some validation


<asp:TextBox ID="txtFromDate" CssClass="Textbox" runat="server" ValidationGroup="Report"
Width="120px" onkeyup="if(this.value != ''){this.value = '';confirm('Please select from calendar.');}return false;"></asp:TextBox>

 <asp:RequiredFieldValidator ID="rfvtxtFromDate" runat="server" ErrorMessage="Enter From Date"
                        ControlToValidate="txtFromDate" ValidationGroup="Report">*</asp:RequiredFieldValidator>

<cc1:CalendarExtender ID="ceFromDate" runat="server" TargetControlID="txtFromDate"
Format="dd/MM/yyyy"></cc1:CalendarExtender>

On Key Up event


function validateAplabeticValue(that) {
            var re = /[^a-z.\s]/gi;

            if (re.test(that.value)) {
                that.value = that.value.replace(re, '');
                alert('Enter only alphabetic value');
            }
        }

  <asp:TextBox ID="txtExtstakhld" runat="server" 
onkeyup="validateAplabeticValue(this);"
MaxLength="30" Width="300px"></asp:TextBox>

Check javascript enable or not


 <noscript>
        <div>
            You must enable javascript to continue.
        </div>
    </noscript>

if you are using old browser then run the below script
 <!--[if lt ie 9]>
<script type="text/javascript">
alert("You are using old IE version, Please upgrade to the latest");
         window.location="http://windows.microsoft.com/en-IN/internet-explorer/download-ie";
</script>
<![endif]-->


Allow Only Alphabets


var specialKeys = new Array();
specialKeys.push(0); //Tab for Firefox
specialKeys.push(8); //Backspace
specialKeys.push(9); //Tab
specialKeys.push(32); //Space
function onlyAlphabets(e, t) {
    try {
        if (window.event) {
            var charCode = window.event.keyCode;
        }
        else if (e) {
            var charCode = e.which;
        }
        else { return true; }
        if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
            return true;
        else {

            for (var num in specialKeys) {
                if (charCode == specialKeys[num])
                    return true;
            }
            return false;
        }
    }
    catch (err) {
        alert(err.Description);
    }
}


Allow Only Alphabets


function onlyAlphabetsNoSpace(e, t) {
    try {
        if (window.event) {
            var charCode = window.event.keyCode;
        }
        else if (e) {
            var charCode = e.which;
        }
        else { return true; }
        if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
            return true;
        else {
            for (var num in specialKeys) {
                if (specialKeys[num] == 32)
                    return false;
                if (charCode == specialKeys[num])
                    return true;
            }
            return false;
        }
    }
    catch (err) {
        alert(err.Description);
    }
}


<asp:TextBox runat="server" ID="txtidfirstname" MaxLength="12" CssClass="txt-box-full" 
onkeypress="return onlyAlphabetsNoSpace(event,this);" ondrop="return false;" onpaste="return false;" />


 function validateAplabeticValue(that) {
            var re = /[^a-z.\s]/gi;

            if (re.test(that.value)) {
                that.value = that.value.replace(re, '');
                alert('Enter only alphabetic value');
            }
        }

Allow alphanumeric


function IsAlphaNumeric(e) {

    var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
    var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
    if (ret) {
        return true;
    } else {
        for (var num in specialKeys) {
            if (keyCode == specialKeys[num])
                return true;
        }
        return false;
    }
}


Allow Decimal Number


function isNumberDecimalKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    } else {
        // If the number field already has . then don't allow to enter . again.
        if (evt.target.value.search(/\./) > -1 && charCode == 46) {
            return false;
        }
        return true;
    }
}


Allow Number


function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    //if (charCode > 31 && (charCode < 48 || charCode > 57)) {
       
    //    return false;
    //}
    //return true;
    if (charCode > 47 && charCode < 58) {
        return true;
    } else {
        for (var num in specialKeys) {
            if (charCode == specialKeys[num])
                return true;
        }
        return false;
    }
}

Change the Value of other control


    function ddlPan() {
        var ddl = document.getElementById("<%=ddlPAN.ClientID%>");
        var SelVal = ddl.options[ddl.selectedIndex].value;
        if (SelVal == "N" || SelVal == "") {
            document.getElementById("<%=txtPANNumber.ClientID%>").value = "";
            document.getElementById("<%=txtPANNumber.ClientID%>").disabled = true;
        }
        else
            document.getElementById("<%=txtPANNumber.ClientID%>").disabled = false;
    }


    <asp:DropDownList runat="server" ID="ddlPAN" CssClass="dropdown-box-full" onchange="ddlPan()">

No comments: