Form Validation Question.

D

Drew

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

Erwin Moller

Drew said:
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
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
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.
{
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;
}
}

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

Exactly 8, it seems.
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.
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.
 
G

Grant Wagner

Erwin said:
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
}

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
M

Mick White

Drew said:
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
 
E

Erwin Moller

Drew said:
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
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top