- Back to Home »
- JAVASCRIPT »
- Javascript Validation
<asp:Button ID="btnsav" class="form_submit" runat="server" Text="Save" OnClick="btnsav_Click1" OnClientClick="return validate()" />
Require field validation:
*********************************************************************************
if (document.getElementById("<%=txtFirstname.ClientID %>").value == "") {
alert("Please enter the first name");
document.getElementById("<%=txtFirstname.ClientID %>").focus()
return false;
}
*******************************************************************************
compare Validation:
if (document.getElementById("<%= hdnOldPassword.ClientID %>").value != "0") {
if (document.getElementById("<%=txtNewPassword.ClientID %>").value != document.getElementById("<%=txtRetypePassword.ClientID %>").value) {
alert("Re-enter password does not match")
document.getElementById("<%=txtNewPassword.ClientID %>").focus()
return false;
}
}
******************************************************************************
Email Validation:
if (document.getElementById("<%=txtEmail.ClientID %>").value=="")
{
alert("Please enter the email");
document.getElementById("<%=txtEmail.ClientID %>").focus()
return false;
}
var exp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
if (!exp.test(document.getElementById("<%=txtEmail.ClientID %>").value)) {
alert("Please enter valid Email");
document.getElementById("<%=txtEmail.ClientID %>").value = "";
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
*****************************************************************************
Drop down Validation:
if (document.getElementById("<%=ddlMonth.ClientID %>").value == "0") {
alert("Please select the month");
document.getElementById("<%=ddlMonth.ClientID %>").focus()
return false;
}
******************************************************************************
Zip Code Validation:
if (Trim(document.getElementById("<%=txtZip.ClientID%>").value) != "") {
// alert("");
var numberFormat = /^\d{0,15}$/;
if (document.getElementById("<%=txtZip.ClientID%>") != null) {
if (document.getElementById("<%=txtZip.ClientID%>").value != '') {
if (!numberFormat.test(document.getElementById("<%=txtZip.ClientID%>").value)) {
alert('Number Only Allowed.');
document.getElementById("<%=txtZip.ClientID%>").value = '';
document.getElementById("<%=txtZip.ClientID%>").focus();
return false;
}
}
}
var zip = document.getElementById("<%=txtZip.ClientID %>").value;
if (zip.length < "5") {
alert("Zip Code should not be below 5 number");
return false;
}
}
*********************************************************************************
Phone Number Validation:
if (Trim(document.getElementById("<%=txtPhone.ClientID%>").value) != "") {
var objRegExp = /^[1-9]\d{2}-\d{3}-\d{4}$/;
if (objRegExp.test(document.getElementById("<%=txtPhone.ClientID %>").value)) {
}
else {
alert("Please enter the valid phone No.");
document.getElementById("<%=txtPhone.ClientID %>").focus()
return false;
}
}
*********************************************************************************
Check Box Validation:
if (document.getElementById("chkTerms").checked == false) {
alert("Please accept the Terms and Conditions");
document.getElementById("chkTerms").focus()
return false;
}
********************************************************************************