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

function problem

P: n/a
This is probably a no brainer, but I am having no luck at getting this to
work. I'm creating functions that will validate form data fields. For
example, the following function:

function checkText(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535 }
if (!field.value || field.value.length < min || field.value.max > max ||
!field.value.match(/^[a-zA-Z]*$/)) {
alert(msg);
field.focus();
field.select();
return false;
}return true;
}

I tested the above function and it works great. The problem is I have
several different functions that will check for fields with different data
like: numbers only, selections in drop down menus, valid zip code, etc. I'm
trying to create one function that I can call using the onSubmit event that
will go through each function to validate all the fields in the form.

I was trying to use the following function to accomplish this using just the
first function. I would include other if statements for the other
functions.

function formValidation(thisform) {
if (checkText(this.first_name, 'Please enter your first name.', 2,
15)==false) {
return true;
}
return false;
}

then within the form tag, I called the function: onSubmit="return
formValidation(thisform)

I'm not all that proficient with Javascript as I'm still learning. Can
anyone identify my mistake in the formValidation function? The argument
"thisform" doesn't need to be the name of the HTML form does it? I thought
it could be any name of my choosing? Any help is greatly appreciated.
Thanks in advance.
-D-

Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Lee
Dwayne said:
function formValidation(thisform) {
if (checkText(this.first_name, 'Please enter your first name.', 2,
15)==false) {
return true;
}
return false;
}

then within the form tag, I called the function: onSubmit="return
formValidation(thisform)

I'm not all that proficient with Javascript as I'm still learning. Can
anyone identify my mistake in the formValidation function? The argument
"thisform" doesn't need to be the name of the HTML form does it? I thought
it could be any name of my choosing? Any help is greatly appreciated.
Thanks in advance.


In the line:
function formValidation(thisform) {
the "formal parameter" can be any name of your choosing,
but you have to use it consistently. In the next line
you refer to "this.first_name". That should be
"thisform.first_name".

In the onSubmit attribute, on the other hand, the "actual
parameter" is not any name of your choosing. In this
particular case, it should be "formValidation(this)",
because the attribute named "this" is a reference to the
form that is being submitted.

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.