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

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

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 -

Scrollable Gridview with fixed headers in asp.net

horizontal and vertical scrollable Gridview with static header row.fixed header in gridview in asp.net.static header in gridview through java script.I was looking for a solution for this for a long time and found most of the answers are not working or not suitable for my situation i also find most of the java script code for that they worked but only with the vertical scroll not with the horizontal scroll and also combination of header and rows doesn't match.