Monday, March 2, 2009

Simple form client Validation in JavaScript

function mywholeform(){
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'white';
document.myform.email.style.background = 'white';
document.myform.phno.style.background = 'white';

if(checkUserName())
{
alert("-enter valid data-");
document.myform.username.focus()
return false;
}else if(checkPwd()){
alert("-enter valid data-");
document.myform.pwd.focus()
return false;
}else if(checkUserFirstName())
{
alert("-enter valid data-");
document.myform.firstname.focus()
return false;
}else if(checkUserLastName())
{
alert("-enter valid data-");
document.myform.lastname.focus()
return false;
}
else if(checkEmail()){
alert("-enter valid data-");
document.myform.email.focus()
return false;
}else if(checkPhno()){
alert("-enter valid data-");
document.myform.phno.focus()
return false;
}

else{
alert("- u have enterd valid data-");
return true;
}
}
function checkUserName(){
var name=document.myform.username.value;

if (name == "")
{

alert("-1-");
document.myform.username.style.background = 'Yellow';
return true;
}

else if ((name.length < 4) || (name.length > 10))
{
alert("-2-");
document.myform.username.style.background = 'Yellow';
return true;
}else if (name.match(/[\<\>!@#\$%^&\*,]+/i) ) {
document.myform.username.style.background = 'Yellow';
alert("-3-");

return true;

}else if (name.match(/[\<\>!@#\$%^&\*,]+/i) ) {
document.myform.username.style.background = 'Yellow';
alert("-3-");

return true;
}


else return false;
}
function checkPwd(){
var pwd=document.myform.pwd.value;

if (pwd == "")
{
alert("-4-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'Yellow';
return true;
}

else if ((pwd.length < 4 ) || (pwd.length > 10))
{
alert("-5-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'Yellow';
return true;

}
else if (pwd.match(/[\<\>!@#\$%^&\*,]+/i) )
{
alert("-6-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'Yellow';
return true;
}

else if (!((pwd.search(/[a-z]+/) > -1) && (pwd.search(/[A-Z]+/) > -1) && (pwd.search(/[0-9]+/) > -1)))
{
alert("-7-");

return true;

}
else return false;

}
function checkUserFirstName(){

var name=document.myform.firstname.value;

if (name == "")
{
alert("-8-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'Yellow';
return true;
}

else if ((name.length < 4) || (name.length > 10))
{
alert("-9-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'Yellow';
return true;
}else if (name.match(/[\<\>!@#\$%^&\*,]+/i) ) {

alert("-10-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'Yellow';
return true;

}else if (name.match(/[\<\>!@#\$%^&\*,]+/i) ) {

alert("-11-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'Yellow';
return true;
}


else return false;
}
function checkUserLastName(){

var name=document.myform.lastname.value;

if (name == "")
{
alert("-12-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'Yellow';
return true;
}

else if ((name.length < 4) || (name.length > 10))
{
alert("-13-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'Yellow';
return true;
}else if (name.match(/[\<\>!@#\$%^&\*,]+/i) ) {

alert("-14-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'Yellow';
return true;

}else if (name.match(/[\<\>!@#\$%^&\*,]+/i) ) {

alert("-15-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'Yellow';
return true;
}


else return false;
}

function checkEmail(email){
var email=document.myform.email.value;

if (email == "")
{
alert("-16-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'white';
document.myform.email.style.background = 'Yellow';

return true;
}

else if ((email.length < 6 ) || (email.length > 20))
{
alert("-17-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'white';
document.myform.email.style.background = 'Yellow';

return true;

}

else if (email.match(/[\<\>!#\$%^&\*,]+/i))
{
alert("-18-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'white';
document.myform.email.style.background = 'Yellow';

return true;
}client


else return false;
}
function checkPhno(phno){
var phno=document.myform.phno.value;
if (phno == "")
{
alert("-19-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'white';
document.myform.email.style.background = 'white';
document.myform.phno.style.background = 'Yellow';

return true;
}
else if (isNaN(parseInt(phno)))
{
alert("-20-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.email.style.background = 'white';
document.myform.phno.style.background = 'Yellow';
return true;
}

else if (!(phno.length == 10))
{
alert("-21-");
document.myform.username.style.background = 'white';
document.myform.pwd.style.background = 'white';
document.myform.firstname.style.background = 'white';
document.myform.lastname.style.background = 'white';
document.myform.email.style.background = 'white';
document.myform.phno.style.background = 'Yellow';
return true;
}
else return false;
}

No comments: