Java Script Form Validation- ASP.Net Web Form

Java Script is the best way to validate a web form at client side and ensure the most accurate data to store from an HTML or asp.net web forms.

In this blog, I will explain about all kind of validation that is used in an ASP.Net and HTML forms

Java Script Form Validation in ASP.Net

var regexMobile = /^[7-9][0-9]{9}$/;
var regexNumberCount = /^d{2}$/;
var regexpincode = /^[1-9][0-9]{5}$/;
var regexemailid = /^w+([-+.’]w+)*@w+([-.]w+)*.w+([-.]w+)*/;
var name = document.getElementById(‘txtInput’);
var country = document.getElementById(‘ddlCountry’);
var digit = document.getElementById(‘txtDigit’);
var mobile = document.getElementById(‘txtMobile’);
var email = document.getElementById(‘txtEmail’);



 if (name.value == “Name” || name.value.length == 0) {
 alert(‘Please enter name’);
name.focus(); return false;
}

if (country.value == “Select” || country.value.length == 0) {
                alert(‘Please select Country/region’);
                country.focus();
                return false;

            }


if (digit.value.search(regexNumberCount) == -1) {
                alert(‘Please enter valid 2 digit’);
                digit.focus();
                return false;
            }

if (mobile.value.search(regexMobile) == -1) {
                alert(‘Mobile number should be 10 digit’);
                mobile.focus();
                return false;


            }

if (email.value.search(regexemailid) == -1) {
                alert(‘Invalid Email id. An email id must contain @ sign’
                email.focus();
                return false;


            }

 Java Script to allow only digit in a field like age, phone 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;
        }

<input name=”” id=”txtMobileNumbertype=”numberonkeypress=”return isNumber(event)placeholder=”Enter 10 digit number“>

Java Script/JQuery to allow alphabet only for a field like name, full name.


      $(document).ready(function () {

            $(“#txtname”).keypress(function (event) {
                var inputValue = event.which;
                // allow letters and whitespaces only.
                if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
                    event.preventDefault();
                }
            });
        });


<input name=”” id=”txtFullNametype=”textplaceholder=”Full Name“>



In some cases Alphabet validation does not work in mobile like Android and IOS, in this case use below function.



<input name=”” id=”txtFullNameonkeyup=”alphabetOnly(event);type=”textplaceholder=”Name“>


function alphabetOnly(e) {

            e.target.value = e.target.value.replace(/[^a-z A-Z]/g, ”);

            return false;

        }






Leave a Comment

RSS
YouTube
YouTube
Instagram