473,763 Members | 9,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="javas cript">
<!--
function Check(form)
{
if (form.PersonID. value == "" || form.PersonID.v alue.length != 8)
{
alert("Please include an ID that is 8 characters");
form.PersonID.f ocus();
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 1936
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="javas cript">
<!--
function Check(form)
{
if (form.PersonID. value == "" || form.PersonID.v alue.length != 8)
{
alert("Please include an ID that is 8 characters");
form.PersonID.f ocus();
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.substrin g(from, to)
so:
testvar = "Hello!";
firstletter = testvar.substri ng(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="javas cript">
<!--
function Check(form)
{
if (form.PersonID. value == "" || form.PersonID.v alue.length != 8)
{
alert("Please include an ID that is 8 characters");
form.PersonID.f ocus();
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.substrin g(from, to)
so:
testvar = "Hello!";
firstletter = testvar.substri ng(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.v alue.length != 8 ||
form.PersonID.v alue.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.v alue.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.Pers onID.value)) {
// PersonID isn't valid
}

--
| Grant Wagner <gw*****@agrico reunited.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.Pers onID.value) ) {
alert("Please include an ID that is p followed by seven numbers");
form.PersonID.f ocus();
return false;
}

....
}
</script>

Mick
Jul 23 '05 #4
JRS: In article <40************ ***********@new s.optusnet.com. au>, seen
in news:comp.lang. javascript, Drew <dr**@fake.co m> 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="javas cript"> // deprecated
function Check(form)
{
if (form.PersonID. value == "" || form.PersonID.v alue.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.f ocus();
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.demo n.co.uk/js-valid.htm>; use such as
OK = /^p\d{7}$/.test(form.Pers onID.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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 javascriptquest ion.
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
8756
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 if they left any field blank. My guess is that someone is trying to hack the site using the form to gain entry or run commands -- I don't really know since I'm not a hacker. I just know that forms are often susceptible to these kinds of...
6
4341
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 developers, but is relevant discussion. i'm not OT here unless someone thinks i'm trolling (which i'm not, obviously). then i'll disappear and never show my face again. :P
2
2367
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 message to tell the user if all the fields have been fill-out. If the user has missed some information the form re-displays with red "alerts" indicating where the user have missed the information while re-populating the information the user has...
16
2250
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 individual fields, such as an "Email Validation Script" or a "Phone Validation Script". Is it ok to put all these scripts on page as they are or should they be joined in some way together to be one script? I'm a total JavaScript newbie and am completely...
1
1631
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 range, which at the lower end is >= 0 and the upper limit is configurable, these totals are then submitted to a web service which posts them to a data store. The question is regarding validation, obviously I can validate on the web form using...
9
4180
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 honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation (e.g. this is numeric, this is alpha-numeric, etc.)
27
4753
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 appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
11
3000
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 certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
18
5808
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 script code being executed having a flaw: function anonymous() { if (!ValidatedTextBoxOnKeyPress(event)) { event.cancelBubble = true; if (event.stopPropagation) event.stopPropagation(); return false; } }
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.