Skip to main content

Validating all type of controls in a form using javascript Forms Validation

Form Validation in Javascript

 

//**************Blank check validation*********************

        function check_blank(fldid, msgid) {
            var fld = document.getElementById(fldid);
            if (fld.value.length == 0 || fld.value.charAt(0) == "
") {
 
                document.getElementById(msgid).innerHTML = "The required field has not been filled in";
                //document.getElementById(error_icon_star).innerHTML = '<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML = '<b style="color:Red">*</b>';
            return true;
        }


        //**************Email formate validation*********************

        function validate_email(fieldid, msgid, error_icon_star) {
            var field = document.getElementById(fieldid);
            with (field) {
                if (check_blank(fieldid, msgid)) {
                    apos = value.indexOf("@");
                    dotpos = value.lastIndexOf(".");
                    if (apos < 1 || dotpos - apos < 2) {
                        document.getElementById(msgid).innerHTML = 'please enter correct email';
                        //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';

                        return false;
                    }
                    else {
                        document.getElementById(msgid).innerHTML = "&nbsp;";
                        //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
                        return true;
                    }
                }
                return false;
            }
        }




        //**************MobileNo 10 digit validation*********************

        function ValidateMobileNumber(fldid, ErrorMsgid) {
            var fld = document.getElementById(fldid);
            var error = "";
            var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

            if (fld.value == "") {
                error = "You didn't enter a phone number.\n";
            } else if (isNaN(stripped)) {
                error = "The phone number contains illegal characters.\n";
            } else if (!(stripped.length == 10)) {
                error = "The phone number is the wrong length. Make sure you included an area code.\n";

            }

            if (error.length > 0) {
                document.getElementById(ErrorMsgid).innerHTML = error;
                //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(ErrorMsgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
            return true;
        }





        //**************ConfirmPassword validation*********************

        function confirm_password(pwd_fieldid, conpwd_fieldid, msgid) {
            var pwd = document.getElementById(pwd_fieldid).value;
            var conpwd = document.getElementById(conpwd_fieldid).value;
            if (pwd != conpwd) {
                document.getElementById(msgid).innerHTML = 'Your passwords do not match. Please type more carefully.';
                //document.getElementById(error_icon_star).innerHTML = '<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            // document.getElementById(error_icon_star).innerHTML = '<b style="color:Red">*</b>';
            return true;
        }





        //**************Radiobuttonlist validation*********************

        function validateRadioButtonList(radiobuttonlist_id, msgid) {
            var listItemArray = document.getElementById(radiobuttonlist_id);
            var isItemChecked = false;

            for (var i = 0; i < listItemArray.rows.length; i++) {
                var listItem = document.getElementById(radiobuttonlist_id + "_" + i);
                if (listItem.checked) {
                    isItemChecked = true;
                }
            }

            if (isItemChecked == false) {
                document.getElementById(msgid).innerHTML = "Please select atleast oneitem";
                //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';

                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
            return true;
        }





        //**************dropdown validation*********************

        function Dropdown_Validation(fieldid, msgid) {
            var empty = document.getElementById(fieldid).value;
            if (empty == "--Select--") {
                document.getElementById(msgid).innerHTML = "Please Select An Item";
                //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
            return true;
        }

       //******************Save button validation to all page***********

        function form_submit() {
            var fname = check_blank("<%=fname.ClientID %>", "fnamemsg");
            var lname = check_blank("<%=lname.ClientID %>", "lnamemsg");
            var username = check_blank("<%=username.ClientID %>", "usernamemsg");
            var password = check_blank("<%=password.ClientID %>", "passwordmsg");

            var confirm = confirm_password("<%=password.ClientID %>", "<%=passwordmatch.ClientID %>", "passwordmatchmsg");
            var type = validateRadioButtonList("<%=Rblist.ClientID %>", "typemsg");
            var email = validate_email("<%=email.ClientID %>", "emailmsg");
            var phone = ValidateMobileNumber("<%=phone.ClientID %>", "phonemsg");
            var country = Dropdown_Validation("<%=country.ClientID %>", "countrymsg");

            var state = check_blank("<%=state.ClientID %>", "statemsg");
            var city = check_blank("<%=city.ClientID %>", "citymsg");
            var address = check_blank("<%=address.ClientID %>", "addressmsg");

            if (fname && lname && username && password && confirm && type && email && phone && state && city && address) {
                return true;
            }
            return false;
        }

Popular posts from this blog

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.