All Type Coding

Search Here

Valid Mobile number validation

 function validateMobileNo(obj, evt) {

            var txtMblNo = obj.id;
            var MobileNo = $('#' + txtMblNo).val();
            var mob = /^[7-9]{1}[0-9]{9}$/;

            if (MobileNo.length < 10) {
                alert("Please enter 10 digits mobile number");
                $('#' + txtMblNo).val('');
                $('#' + txtMblNo).focus();
                return false;
            }

            if (mob.test(obj.value) == false) {
                alert("Please enter valid mobile number.");
                $('#' + txtMblNo).focus();
                $('#' + txtMblNo).val('');
                return false;
            }

        }

Email validation

 function validateEmailId(obj, evt) {

            var txtEmail = obj.id;
            var Email = $('#' + txtEmail).val();

            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            if (Email.match(mailformat)) {
                return true;
            }
            else {

                alert("Invalid email id entered! Please enter valid email id");
                $('#' + txtEmail).val('');
                $('#' + txtEmail).focus();
                return false;
            }
        }

How to unckeck checkboxlist based on last checkbox selection.?

<script type="text/javascript">


        $(document).ready(function () {
            $('#ContentPlaceHolder1_chk').live('click', function () {
                var values = '';
                var $ctrls = document.getElementById('ContentPlaceHolder1_chk').getElementsByTagName("input");
                $('#ContentPlaceHolder1_chk input[type=checkbox]:checked').each(function () {
                    if (values.length == 0) {
                        values = $('label[for=' + this.id + ']').html();
                    }
                    else {
                        values += "," + $('label[for=' + this.id + ']').html();
                    }

                });
                var strarray = values.split(',');
                for (var i = 0; i < strarray.length; i++) {
                    if (strarray[i] == "NA") {
                        var selValue = [1, 2, 3, 4, 5, 6, 7, 8, 9];
                        for (var j = 0; j < selValue.length; j++) {

                            $('input:checkbox[value=' + selValue[j] + ']').removeAttr('checked');
                        }
                    }
                }

            });
        });

    </script>

Numeric validation in javascript


function isNumber(e) {
            var keynum;

            keynum = e.keyCode ? e.keyCode : e.which;
            if ((keynum > 47 && keynum < 58) || keynum == 98) {

                return true;
            }
            else {
                e.preventDefault();
                alert('Please enter only numbers.');
                return false;
            }


        }

Alphabets validation in javascript

For Dynamic control
function isCharKey(obj, evt) {
            var ctrl = obj.id;
            var charCode1 = evt.which;
            if (charCode1 > 31 && charCode1 != 32 && charCode1 != 45 && (charCode1 < 65 || charCode1 > 90) && (charCode1 < 97 || charCode1 > 122)) {

                alert("Please enter alphabets only");

                $('#' + ctrl).focus();

                // $('#' + ctrl).val('');
                return false;
            }
            return true;
        }

For static control

 function isalphanumneric(e) {
            var keynum;

            keynum = e.keyCode ? e.keyCode : e.which;
            //  alert(keynum);
            if ((keynum < 65 || keynum > 90) && (keynum < 97 || keynum > 122) && (keynum != 8 && keynum != 9) && keynum != 46 && keynum != 37 && keynum != 39 && keynum != 32) {
                e.preventDefault();
                alert('Please enter alphabets only');
                return false;
            }

            return true;

        }