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 = " ";
//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 = " ";
//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
= " ";
//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 = " ";
//
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 = " ";
//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 = " ";
//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;
}