HighDots Forums  

Form Validation Question.

Javascript JavaScript language (comp.lang.javascript)


Discuss Form Validation Question. in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Drew
 
Posts: n/a

Default Form Validation Question. - 05-26-2004 , 12:05 AM






I've already created a simple method of ensuring that all form feilds
are filled out before the form is submitted to an ASP page for records
to be added to the data base.

(Sorry about the formating, my newsreader may make it a mess!)

<script language="javascript">
<!--
function Check(form)
{
if (form.PersonID.value == "" || form.PersonID.value.length != 8)
{
alert("Please include an ID that is 8 characters");
form.PersonID.focus();
return false;
}

//There are more checking feilds here as well for phone number etc...

{
return true;
}
}
//-->
</script>

At the moment, that code above just makes sure that the user has entered
at least 8 characters/numbers.

Now what I want to do is make sure that in the PersonID feild that the
user enters only an id that begins with the letter "p" (lowercase only)
followed only by any 7 numbers.

ie. p1234567, p7654321, p2468135 etc...

How do I do this?

Cheers.

Reply With Quote
  #2  
Old   
Erwin Moller
 
Posts: n/a

Default Re: Form Validation Question. - 05-26-2004 , 07:55 AM






Drew wrote:

Quote:
I've already created a simple method of ensuring that all form feilds
are filled out before the form is submitted to an ASP page for records
to be added to the data base.

(Sorry about the formating, my newsreader may make it a mess!)

script language="javascript"
!--
function Check(form)
{
if (form.PersonID.value == "" || form.PersonID.value.length != 8)
{
alert("Please include an ID that is 8 characters");
form.PersonID.focus();
return false;
}

//There are more checking feilds here as well for phone number etc...

{
return true;
}
}
//--
/script

At the moment, that code above just makes sure that the user has entered
at least 8 characters/numbers.

Now what I want to do is make sure that in the PersonID feild that the
user enters only an id that begins with the letter "p" (lowercase only)
followed only by any 7 numbers.

ie. p1234567, p7654321, p2468135 etc...

How do I do this?

Cheers.
use String.substring(from, to)
so:
testvar = "Hello!";
firstletter = testvar.substring(0,1);


Regards,
Erwin Moller


Reply With Quote
  #3  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Form Validation Question. - 05-26-2004 , 08:29 AM



JRS: In article <40b4179f$0$31678$afc38c87 (AT) news (DOT) optusnet.com.au>, seen
in news:comp.lang.javascript, Drew <drew (AT) fake (DOT) com> posted at Wed, 26 May
2004 14:05:56 :
Quote:
I've already created a simple method of ensuring that all form feilds
are filled out before the form is submitted to an ASP page for records
to be added to the data base.

script language="javascript"
// deprecated

Quote:
function Check(form)
{
if (form.PersonID.value == "" || form.PersonID.value.length != 8)
Since you test the length to be 8, ISTM unnecessary to test the empty
case first.

Quote:
{
alert("Please include an ID that is 8 characters");
form.PersonID.focus();
return false;
}

//There are more checking feilds here as well for phone number etc...

{
return true;
}
}

Quote:
At the moment, that code above just makes sure that the user has entered
at least 8 characters/numbers.
Exactly 8, it seems.

Quote:
Now what I want to do is make sure that in the PersonID feild that the
user enters only an id that begins with the letter "p" (lowercase only)
followed only by any 7 numbers.
You mean one number of seven (decimal) digits.

Quote:
ie. p1234567, p7654321, p2468135 etc...

How do I do this?
See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>; use such as
OK = /^p\d{7}$/.test(form.PersonID.value)
if (!OK) alert("Aaargh!")
return OK

That page also has parameter-driven validation, which enables brief
expression of multiple tests on multiple fields.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<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
  #4  
Old   
Grant Wagner
 
Posts: n/a

Default Re: Form Validation Question. - 05-26-2004 , 10:02 AM



Erwin Moller wrote:

Quote:
Drew wrote:

I've already created a simple method of ensuring that all form feilds
are filled out before the form is submitted to an ASP page for records
to be added to the data base.

(Sorry about the formating, my newsreader may make it a mess!)

script language="javascript"
!--
function Check(form)
{
if (form.PersonID.value == "" || form.PersonID.value.length != 8)
{
alert("Please include an ID that is 8 characters");
form.PersonID.focus();
return false;
}

//There are more checking feilds here as well for phone number etc...

{
return true;
}
}
//--
/script

At the moment, that code above just makes sure that the user has entered
at least 8 characters/numbers.

Now what I want to do is make sure that in the PersonID feild that the
user enters only an id that begins with the letter "p" (lowercase only)
followed only by any 7 numbers.

ie. p1234567, p7654321, p2468135 etc...

How do I do this?

Cheers.

use String.substring(from, to)
so:
testvar = "Hello!";
firstletter = testvar.substring(0,1);

Regards,
Erwin Moller
var s = 'p12345678';
alert(s.charAt(0));

However, your validation as it stands would allow me to enter " " and it
would be valid. Even if you add validation to ensure the first letter is "p" to
what you already have:

if (form.PersonID.value == "" || form.PersonID.value.length != 8 ||
form.PersonID.value.charAt(0) != "p")

I could still enter "p ".

You may want to consider implementing the trim() functionality available from
this newsgroup's FAQ <url: http://jibbering.com/faq/#FAQ4_16 /> to help with
your validation:

var personId = form.PersonID.value.trim();
if (personId == "" || personId != 8 || personId.charAt(0) != "p")

or, you could perform your validation with regular expressions, which will
ensure an exact match:

if (!/p\d{7}/.test(form.PersonID.value)) {
// PersonID isn't valid
}

--
Quote:
Grant Wagner <gwagner (AT) agricoreunited (DOT) com
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html




Reply With Quote
  #5  
Old   
Mick White
 
Posts: n/a

Default Re: Form Validation Question. - 05-26-2004 , 12:01 PM



Drew wrote:
Quote:
Now what I want to do is make sure that in the PersonID feild that the
user enters only an id that begins with the letter "p" (lowercase only)
followed only by any 7 numbers.

ie. p1234567, p7654321, p2468135 etc...

How do I do this?

function Check(form){
if (!/^p{\d}7$/.test(form.PersonID.value) ) {
alert("Please include an ID that is p followed by seven numbers");
form.PersonID.focus();
return false;
}

....
}
</script>

Mick


Reply With Quote
  #6  
Old   
Drew
 
Posts: n/a

Default Re: Form Validation Question. - 05-27-2004 , 10:07 PM



Thanks for the help guys!

Reply With Quote
  #7  
Old   
Erwin Moller
 
Posts: n/a

Default Re: Form Validation Question. - 05-28-2004 , 04:28 AM



Drew wrote:

Quote:
Thanks for the help guys!
You are welcome.

You have now three ways of doing it: substring, charAt, and a regular
expression. Make your pick.

Please bookmark this:
http://www.jibbering.com/faq/

and check it before you have post a javascriptquestion.
I estimate 90% of the questions asked here is answered there.
Hence the name FAQ. :P

Regards,
Erwin Moller


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.