View Full Version : validate using loop
ahmedsoliman
12-09-2002, 03:31 PM
i am using the following code to perform validation on textbox,
but i have16 text box, how to creat validation on all boxes using loop. the boxes nemed prs1,prs2,prs3,......
if (addform.prs1.value !="")
{
var x=document.addform.prs1.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("Please input a valid number in size price!")
return false
document.addform.prs1.focus
}
}
DoubleV
12-09-2002, 04:41 PM
for (i=0; i<17; i++) {
if (addform["prs"+i].value !="") {
var x=document.addform["prs"+i].value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x)) {
testresult=true
}
else{
alert("Please input a valid number in size price!")
return false
document.addform["prs"+i].focus
}
}
}
beetle
12-09-2002, 05:54 PM
Your regex is a bit more complicated than it needs to be
var anum = /^\d+(\.\d+)?$/
is more proper.
ahmedsoliman
12-09-2002, 10:50 PM
thank you douplev, to beetle: idon't understand my statment so i don't understan your statment which is easier than my own.
what does d $/ mean?
beetle
12-10-2002, 12:25 AM
Well, I can't really teach regular expressions in a single thread, but here are some helpful links for understanding regular expressions.
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/regexp.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsjsgrpRegExpSyntax.asp
http://www.webreference.com/js/column5/rules.html
ahmedsoliman
12-10-2002, 01:50 AM
it gives me an error:addform[...].value is null or not an opject
beetle
12-10-2002, 01:54 AM
should be
document.addform.elements["prs"+i].focus();
whammy
12-10-2002, 06:37 AM
P.S. Don't let regular expressions scare you... surprisingly many developers don't even have a working knowledge of them. If they just knew how easy they are....
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.