By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,848 Members | 3,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,848 IT Pros & Developers. It's quick & easy.

validating two forms

P: n/a
I have put two snips of code I use in the body section of my page to
validate and submit two forms.

The first snip worked fine until I included the second...is the
problem the fact variables have the same name?

Cheers,

Chris

<script type="text/javascript">
var field1 = new LiveValidation( 'field1', {onlyOnSubmit: true } );
field1.add( Validate.Presence );

var mySelect1 = new LiveValidation('mySelect1');
mySelect1.add(Validate.Exclusion, { within: ['Please select...'],
failureMessage: "x"});

var automaticOnSubmit = field1.form.onsubmit;
field1.form.onsubmit = function(){
var valid = automaticOnSubmit();
...........
}
</script>
------------------------------------------------
<script type="text/javascript">
var field2 = new LiveValidation( 'field2', {onlyOnSubmit: true } );
field2.add( Validate.Presence );

var mySelect2 = new LiveValidation('mySelect2');
mySelect2.add(Validate.Exclusion, { within: ['Please select...'],
failureMessage: "x"});

var automaticOnSubmit = field1.form.onsubmit;
field2.form.onsubmit = function(){
var valid = automaticOnSubmit();
.............
}
</script>
Sep 20 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
SAM
Chris a écrit :
I have put two snips of code I use in the body section of my page to
validate and submit two forms.
That smells prototype.js or something like that, no ?

An handmade JS script to validate a form is not so difficult to do.

function validate(f) {
f = f.elements, n = f.length;
for(var i=0; i<n; i++) {
if(f[i].type == 'radio') {
var r = f[f[i].name], z = r.length, ok = false;
for(var j=0; j<z; j++) if(r[j].checked) ok = true;
i += j;
if(!ok) {
alert('Choice something in : '+f[i].name);
r[0].focus();
return false;
}
}
if((f[i].type == 'text' || f[i].tagName == 'textarea') &&
f[i].value.length <1) {
alert('Complete field : '+f[i].name);
f[i].focus();
return false;
}
if(f[i].tagName == 'select') {
var k = f[i].selectedIndex;
if(k==0) {
alert('Forgiven the choice : '+f[i].options[0].text);
f[i].focus();
return false;
}
}
}
return true;
}
window.onload = function() {
document.forms[0].onsubmit = function(){ return validate(this); };
document.forms[1].onsubmit = function(){ return validate(this); };
}
The first snip worked fine until I included the second...is the
problem the fact variables have the same name?
Probably ?
--
sm
Sep 20 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.