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

Form Validation Question.

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
Erwin Moller wrote:
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
}

--
| Grant Wagner <gw*****@agricoreunited.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
Jul 23 '05 #3

P: n/a
Drew wrote:
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
Jul 23 '05 #4

P: n/a
JRS: In article <40***********************@news.optusnet.com.au> , seen
in news:comp.lang.javascript, Drew <dr**@fake.com> posted at Wed, 26 May
2004 14:05:56 :
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.

--
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.
Jul 23 '05 #5

P: n/a
Thanks for the help guys!
Jul 23 '05 #6

P: n/a
Drew wrote:
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
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.