473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Form Validation Question.

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
6 1903
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
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
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
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
Thanks for the help guys!
Jul 23 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
2
by: Tim Mills | last post by:
The following code asks the user to sumbit a name, email address, and some text for a quotation via a FORM. I have written a javascript function to evaluate the fields in the form and pop-up a...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
1
by: Colin Basterfield | last post by:
Hi, I have a web form which takes daily sales totals, both counts and monetary value and is done on a weekly basis, so on a Monday morning the User would enter these totals. Each total has a...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
18
by: Axel Dahmen | last post by:
Hi, trying to submit an ASPX form using the key (using IE6) the page is not submitted in my web project. Trying to debug the pages' JavaScript code I noticed that there's some ASP.NET client...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.