Monday, August 11, 2008

Basic Validation using Javascript

var remail=/^([_&a-zA-Z0-9-]+(\.[_&a-zA-Z0-9-]+)*@[&a-zA-Z0-9-]+\.+[&a-zA-Z0-9-]+)/;

/* Check the email address */
function checkEmail(fieldvalue){
if(remail.test(fieldvalue)){
return false;
}
else {
return true;
}
}

/* Trimming the string */
function trim(str)
{
return str.replace(/^\s*|\s*$/g,"");
}

function ValidateUserRegister(formNm)
{
var errMsg = "Please correct the following field(s):\n\n";
var errNum = 0;
var fldName = "";
var passerr = 0;

if(trim(formNm.user_name.value)=="")
{
if( !errNum ) fldName = "user_name";
errMsg += ++errNum + ". Username is a required field.\n";
}

if(trim(formNm.user_email.value)=="")
{
if( !errNum ) fldName = "user_email";
errMsg += ++errNum + ". Email is a required field.\n";
}

if(trim(formNm.user_email.value)!="" && checkEmail(trim(formNm.user_email.value))) {
if( !errNum ) fldName = "user_email";
errMsg += ++errNum + ". Please enter a Valid Email Address.\n";
}

if(errNum)
{
alert(errMsg);
return false;
}

return true;
}

No comments:

Popular Posts