Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > javascript to validate form

Reply
Thread Tools

javascript to validate form

 
 
Mick Walker
Guest
Posts: n/a
 
      08-29-2007
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
 
Reply With Quote
 
 
 
 
Dr J R Stockton
Guest
Posts: n/a
 
      08-29-2007
In comp.lang.javascript message <(E-Mail Removed)>, Wed,
29 Aug 2007 14:34:41, Mick Walker <(E-Mail Removed)> posted:

>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,
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

> 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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript to Validate the Phone Number Field Using Javascript :-) Abhishek Javascript 5 08-07-2008 09:14 AM
submit form, validate form, set cookie, send email, download file mhawkins19@adelphia.net Javascript 1 03-17-2005 08:19 AM
Javascript Validate Form Fields Colin Graham Javascript 1 01-25-2005 06:36 PM
Validate form =?Utf-8?B?UmFqYW5p?= ASP .Net 1 11-13-2004 08:01 AM
form wont validate on remote server Jack Wheeler ASP .Net 1 06-14-2004 09:46 PM



Advertisments