javascript to validate form

M

Mick Walker

Hi,

I have a problem that I have been trying to figure for a couple of days,
and am wondering if anyone out there would be so kind as to give me a
solution. (Deadline time)

I am trying to validate a form. Its quite a simple form, but I am a wee
bit stuck. Baically it consists of 9 text input fields and a select
element. All elements on the form, have to have a value in them
(manditory), BUT 2 items (txtEmail, and txtDOB) have to match a certian
criteria.

The first is obvious, it has to be a valid email address, the second
however is a lot mroe difficult. The dob entered, not only have to be a
valid date, it has to make the user over 18, otherwise the form wont submit.

I am a ASP.Net programmer, but due to time resrictions we are updating
an existing project using classic asp (which I have never used) were it
asp.net I would simply handle this problem server side (lot of resources
I know), but that isnt an option for me. Plus I could do with learning a
little more than the bard Basics of Javascript.

At the moment I am using 3 different function, one to validate the date,
one to validate the email, and another to make sure all elements contain
a value.

These are like so:

function validateDate(fld) {
var RegExPattern =
/^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
var errorMessage = 'Please enter valid date as month, day, and four
digit year.\nYou may use a slash, hyphen or period to separate the
values.\nThe date must be a real date. 30/2/2000 would not be
accepted.\nFormat dd/mm/yyyy.';
if ((fld.value.match(RegExPattern)) && (fld.value!='')) {

} else {
alert(errorMessage);
fld.focus();
return false;
}
}
function validateWinStuffForm(f)
{
if(!valid_Required(f)){
alert('The form was filled out incorrectly');
return false;
}
}
function valid_Email(t) {
if(!r_validEmail.test(t)) {
alert('Please enter a valid email address');
return false;
}
return true;
}

I would like one function, that takes the form as an argument and does
all of these checks. The names of the DOB and Email fields will always
be the same on any form which the function is used (as the forms are
generated server side).

I hope someone can help

Kind Regards
Mick Walker
 
D

Dr J R Stockton

Wed said:
At the moment I am using 3 different function, one to validate the
date,

You are attempting a remarkably complex RegExp-only method. See,
without delay, <URL:http://www.merlyn.demon.co.uk/js-date4.htm>, for
validating a string as a date and reading it. Then check the age by
comparison of as many of Y M D as is necessary, after checking the exact
applicable definition of 18.
one to validate the email,

Don't get carried away : see, likewise,
and another to make sure all elements contain a value.

Literally meaningless - all input controls always have a string value.
You can check that the string is non-empty or non-visibly-blank or long
enough or has only the right sort of characters or ...
var errorMessage = 'Please enter valid date as month, day, and four
digit year.\nYou may use a slash, hyphen or period to separate the
values.\nThe date must be a real date. 30/2/2000 would not be
accepted.\nFormat dd/mm/yyyy.';

Confusing. Is the error that February rarely has a 30th or that 2 is a
single character or both? OTOH, you might as well allow any non-digit
separator, matching with /\s*(\d\d?)\D+(\d\d?)\D+(\d\d\d\d)\s*/ or
similar.
I hope someone can help

Your firm's best move is to hire a javascript programmer if javascript
is to be programmed; also, seek a Thunderbird spelling-checker.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top