/    Sign up×
Community /Pin to ProfileBookmark

Form Validation (Premature)

I have certain fields which I’m having the script confirm upon submission of the form. For some reason, the form tries to validate upon exiting of the 1st field (1st name) before the user has even had a chance to complete the form. Any idea on how to correct this?

URL:
[url]http://www.tolbertworld.net/xconnecttool/xconnectform.htm[/url]

Code:
<script language=”JavaScript” type=”text/JavaScript”>
<!–
function MM_findObj(n, d) {
var p,i,x; if(!d) d=document; if((p=n.indexOf(“?”))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() {
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>

Thanks for the help!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@NedalsSep 30.2005 — If you try to move from one text field to the next using <return/enter>, the form will submit. You need to use the <tab> key.

If that's a problem for you, then you will need to add some more javascript into your validation function to move the focus() from one field to the next, or you can change the <input type="submit"> to <input type="button"> plus an onclick event.

One other thought...

Add a 'return false' if invalid and 'return true' if valid to your function, and in your HTML you will need

onsubmit = "return MM_validateForm()"
Copy linkTweet thisAlerts:
@marcdog64authorSep 30.2005 — ?

I'm not really sure how to apply the 2nd recommendation to my code.

I'm currently using 'onclick'. to call the function.

syntax below.

onClick="MM_validateForm('firstname','','R','lastname','','R','phone','','R','email','','RisEmail','company','','R','carrier','','R','customercage','','RinRange0:51','customercabinet','','RinRange0:99','customerru','','NinRange0:81','ispcage','','RinRange0:51','ispcabinet','','RinRange0:99','ispru','','NinRange0:81');return document.MM_returnValue" value="Submit">
Copy linkTweet thisAlerts:
@NedalsOct 01.2005 — This may not be what you have, but here's the way I would do it..
<i>
</i>function MM_validateForm() {
..
..
if (errors) {
alert('The following error(s) occurred:n'+errors);
document.MM_returnValue = (errors == '');
return false;
}
return true;
}

&lt;form .... onsubmit="return MM_validateForm('firstname',....)"&gt;
...
...
&lt;input type="submit" value="submit"&gt;
&lt;/form&gt;

I would not bother to parse all those items, but rather build an array within your function and loop through that instead of 'args'

One other thing, could you edit your last post and either remove most of the argument list or break it into 2 or three

lines to stop the need for a horrizontal scroll.
×

Success!

Help @marcdog64 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...