...

View Full Version : Please Help with Form Validation



karmakiller2u
05-09-2007, 11:37 AM
Hi I have this code and it works well, but how do I add another check for valid.Surname ???

Please Help


// Validator Object
var valid = new Object();

// REGEX Elements

// matches zip codes
valid.PostCode = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/;

//matches email
valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

// matches phone ###-###-####
//valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
valid.ContactNo = /^0\d{2,4}[ -]{1}[\d]{3}[\d -]{1}[\d -]{1}[\d]{1,4}$/;


function validateForm(theForm) {

var elArr = theForm.elements;

for(var i = 0; i < elArr.length; i++) {

with(elArr[i]) {

var v = elArr[i].validator;

if(!v) continue;

var thePat = valid[v];

var gotIt = thePat.exec(value);

if(! gotIt){
alert(name + ": Please insert a valid " + v + " to " + value);
elArr[i].select();
elArr[i].focus();
return false;
}
}
}

return true;

}

Philip M
05-09-2007, 01:45 PM
valid.surName = /^([A-z]+)([\-\']?)([A-z]+)$/; // only letters, and optional hyphens (Smith-Jones) and apostrophe (O'Brian, B'stard) allowed, returns true if only these characters and no others are encountered in string.

valid.surName = /([^A-z\-\'])/ // only letters, hyphens and apostrophe allowed, returns true if invalid characters encountered in string.

A better email validator is

valid.emailAddress = /^(\w)([a-z0-9_\.\-])+(\w)\@(([a-z0-9\-]?)+\.)+([a-z0-9]{2,4})$/i



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum