Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Form Validation question

3 views
Skip to first unread message

Raphael Gluck

unread,
Jul 26, 2004, 9:18:24 AM7/26/04
to
HI

I am wondering if anyone can help me

I added a little script to a form page, so that a couple of fields have to
filled in. One contains an obligatory field for number data, one for regular
text.
However it seems that when a user will just enter a few spaces in the
required, they seem to be able to pass the validation script.

Here is the script, How should i modify it to exclude spaces?

Many thanks

Raphael

<script language="JavaScript" type="text/JavaScript">
<!--
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2];
val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an
e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number
between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is
required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>


Marcel van den Brink

unread,
Aug 6, 2004, 10:06:33 AM8/6/04
to

"Raphael Gluck" <so...@nospam.com> wrote in message
news:ce30b0$gnh$1...@titan.btinternet.com...


Try this:

It is the same function, but then made more readable. :)


function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

for (i=0; i<(args.length-2); i+=3) {
test=args[i+2];
val=MM_findObj(args[i]);
if (val) {
nm=val.name;

val = val.value; // THIS LINE
CHANGED

val = val.replace(/^\s*|\s*$/g,""); // THIS LINE
CHANGED

if (val!="") { // THIS LINE
CHANGED


if (test.indexOf('isEmail')!=-1) {
p=val.indexOf('@');
if (p<1 || p==(val.length-1))
errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') {
num = parseFloat(val);
if (isNaN(val))
errors+='- '+nm+' must contain a number.\n';

if (test.indexOf('inRange') != -1) {
p=test.indexOf(':');
min=test.substring(8,p);
max=test.substring(p+1);
if (num<min || max<num)
errors+='- '+nm+' must contain a number between '+min+' and
'+max+'.\n';
}
}
} else if (test.charAt(0) == 'R')
errors += '- '+nm+' is required.\n';
}
}

if (errors)
alert('The following error(s) occurred:\n'+errors);

document.MM_returnValue = (errors == '');
}

What I did was to get the value a bit earlier (it was first done in
the 'if' statement. Next line I use a regular expression to substitute
all trailing and starting whitespaces (so, tabs etc. are also removed!)
with nothing. So you'll start with a clean string.

-- Marcel


0 new messages