Below is the code to validate a form. Below function can be call either on a button click or on an anchor link.
function Validation() {
var regexMobile = /^[7-9][0-9]{9}$/;
var regexNumberCount = /^d{10}$/;
var regexpincode = /^[1-9][0-9]{5}$/;
var regexemailid = /^w+([-+.’]w+)*@w+([-.]w+)*.w+([-.]w+)*/;
var name = document.getElementById(‘<%=txtName.ClientID %>’);
var mobile = document.getElementById(‘<%=txtMoile.ClientID %>’);
var address = document.getElementById(‘<%=txtareaAddress.ClientID %>’);
var email = document.getElementById(‘<%=txtEmail.ClientID %>’);
var pincode = document.getElementById(‘<%=txtPincode.ClientID %>’);
var ddlOptions= document.getElementById(‘<%=ddlOptions.ClientID %>’);
var tnc = document.getElementById(‘<%=cb1.ClientID %>’);
if (name.value == “Name” || name.value.length == 0) {
alert(‘Please enter name’);
return false;
}
else if (mobile.value.length == 0) {
alert(‘Please enter mobile’);
return false;
}
else if (mobile.value.search(regexMobile) == -1) {
alert(‘Please enter a valid mobile number’);
return false;
}
else if (address.value == “Address” || address.value.length == 0) {
alert(‘Please enter address’);
return false;
}
else if (email.value.length == 0) {
alert(‘Please enter email id’);
return false;
}
else if (email.value.search(regexemailid) == -1) {
alert(‘Please enter a valid email id’);
return false;
}
else if (pincode.value.length == 0) {
alert(‘Please enter Pin code’);
return false;
}
else if (pincode.value.search(regexpincode) == -1) {
alert(‘Please enter a valid Pin code’);
return false;
}
else if (ddlOptions.selectedIndex == 0) {
alert(‘Please select toothpaste’);
return false;
}
else if (tnc.checked == false) {
alert(‘Please accept terms & conditions’);
return false;
}
return true;
}