PDA

View Full Version : Help with form validation



scohie
01-28-2007, 01:59 PM
I have used http://www.dynamicdrive.com/dynamicindex16/requiredcheck.htm
to successfully validate parts of my form, just wondering if I could get some assistance in validating other functions such as valid email and is numeric?

Thanks

thetestingsite
01-28-2007, 02:36 PM
For the email check, you could combine the script above with the following one:

http://www.dynamicdrive.com/dynamicindex16/emailvalidate.htm
(http://www.dynamicdrive.com/dynamicindex16/emailvalidate.htm)

As for the numbers, I believe there is a function call isNaN (is Not a Number). But not 100% on that.

Hope this helps.

rajug
01-29-2007, 04:16 AM
Here is my email validation function:


function echeck(str) {
var at = "@"
var dot = "."
var lat = str.indexOf(at)
var lstr = str.length
var ldot = str.indexOf(dot)

if (str.indexOf(at) == -1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}
return true
}


And for the numeric, as thetestingsite said, there is isNaN function in javascript:


if(isNaN(value)){
alert('string');
}else{
alert('numeric');
}