onSubmit problem

J

Juan Schwartz

onSubmit of form is set to
"check_phone('fax','billing_group_info','Fax Number');
check_errors();" but when submitted it does not actually run the above
in onsubmit. if you put those at the top of the browser(with
javascript: ") or hardcode them into html tags, they work. I know the
return part is outside of the for loop and would conflict with more
than one form, but it's not working with one form so I haven't
addressed that yet. Any help?


arr_validate = new Array();
err_msg = new Array();

function add_validation(func, field, form_name, field_desc) {
arr_validate.push(new Array(func, field, form_name, field_desc));

}

function check_all_validation() {

if (arr_validate.length > 0) {

for ( i in arr_validate ) {
if(typeof(document.getElementsByName(arr_validate
[2]).onsubmit) == "undefined") {

document.getElementsByName(arr_validate[2]).onsubmit =
arr_validate[0] + '(' + '\''+ arr_validate[1] + '\'' + ',' +'\''
+ arr_validate[2] + '\'' + ',' + '\'' + arr_validate[3] + '\'' +
'); ';

} else {

document.getElementsByName(arr_validate[2]).onsubmit =
document.getElementsByName(arr_validate[2]).onsubmit +
arr_validate[0] + '(' + '\''+ arr_validate[1] + '\'' + ',' +'\''
+ arr_validate[2] + '\'' + ',' + '\'' + arr_validate[3] + '\'' +
'); ';

}

}


document.getElementsByName(arr_validate[2]).onsubmit =
document.getElementsByName(arr_validate[2]).onsubmit + 'return
check_errors();';

alert(document.getElementsByName(arr_validate[2]).onsubmit);

}
}

function check_phone(field, form_name, field_desc) {

var phone_no = document.getElementsByName(field)[0].value;

if( !phone_no.match( '/^((1-?)?.?[0-9]{3}.{0,2}[0-9]{3}.?[0-9]{4}.*)?
$/' ) ) {
err_msg.push('Phone number in field "' + field_desc + '" is
invalid');

}
}

function check_zip(field, form_name, field_desc) {

var zip = document.getElementsByName(field)[0].value;

if( !zip.match( '/^\d{5}$/' ) ) {
err_msg.push('Phone number in field "' + field_desc + '" is
invalid');
}
}

function check_errors() {

if (err_msg.length > 0) {
alert('The following errors occurred: \n\n' + err_msg.join('\n\n'));
return false;
} else {
return true;
}

}
 
T

Thomas 'PointedEars' Lahn

Juan said:
onSubmit of form is set to
"check_phone('fax','billing_group_info','Fax Number');
check_errors();" but when submitted it does not actually run the above
in onsubmit. if you put those at the top of the browser(with
javascript: ") or hardcode them into html tags, they work. I know the
return part is outside of the for loop and would conflict with more
than one form, but it's not working with one form so I haven't
addressed that yet. Any help?

Sorry, your code is a PITA to analyze; you'll be lucky if you find someone
else who considers it. Once you cleaned it up, you are more likely to see
the error for yourself.

0. Read the FAQ and the FAQ Notes: http://jibbering.com/faq/

1. Indent code with 2 or 4 spaces, not tabs. (Especially not when posting;
display will be different among your readers, with some environments
even distorted.)

2. Declare all your identifiers: var arr_validate = new Array();

3. Use "'" instead of '\''; use Array.prototype.join() instead of `+'.

4. Use Function object references and Function.prototype.call()/apply()
instead of concatenated call statements.

5. Avoid inefficient redundancy:

var o = document.getElementsByName(arr_validate[2]);
if (o)
{
// ... o ...
}

6. Make sure your markup is Valid: http://validator.w3.org/

Why should you do that?
http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you

7. Use a debugger like Firebug [alternative: window.alert()] to see
what the actual values are.

8. The `onsubmit' *property* requires a Function object reference
(or `null'), not a string value.


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top