469,927 Members | 1,984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Validating form fields

I want to validate that the data entered by a user is correct before they
submit the form.

How do I validate the following (or what functions/classes do I use):

- Ensure a text field is not empty
- Ensure that a text field contains only numeric values
- Ensure that the contents of a text field is shorter than a certain length
- Remove commas and other formatting from a field before submission

TIA.
Sep 14 '05 #1
8 4269
Davey wrote:
- Ensure a text field is not empty
if (!yourfield.value)
alert("empty")
- Ensure that a text field contains only numeric values
if (yourfield.value.search(/\D/) != -1)
alert("only numbers please")

strip all non numeric values:
var val = yourfield.value.replace(/\D/g,"");

- Ensure that the contents of a text field is shorter than a certain length
if (!yourfield.value.length > 10)
alert("to long")
- Remove commas and other formatting from a field before submission


removing all of the following characters: ",.-+"
var val = yourfield.value.replace(/[,.\-+]/g,"")
// mask the minus as otherwise it will define a range

Checkout any RegularExpression reference of your choice to learn more on
this.

HTH
Daniel
Sep 14 '05 #2

You can use regular expression to do it, it very powerful for user
input validation.

Phal

Sep 14 '05 #3
Daniel Kirsch wrote:
Davey wrote:

[...]
- Ensure that a text field contains only numeric values

if (yourfield.value.search(/\D/) != -1)
alert("only numbers please")

if ( /\D/.test(yourfield.value ) )
[...]
- Ensure that the contents of a text field is shorter than a certain
length

if (!yourfield.value.length > 10)
alert("to long")


The logic here is back-to-front, it will respond that the text is to
long only if it is not greater than 10.

if ( yourfield.value.length > 10 )
alert("it's too long");
[...]
--
Rob
Sep 14 '05 #4
RobG wrote:
Daniel Kirsch wrote:
if (!yourfield.value.length > 10)
alert("to long")

The logic here is back-to-front, it will respond that the text is to
long only if it is not greater than 10.


Autsch. Another copy&paste bug.

Thanks for correction
Daniel
Sep 14 '05 #5
JRS: In article <43**********@x-privat.org>, dated Wed, 14 Sep 2005
12:06:19, seen in news:microsoft.public.scripting.jscript, Davey
<da***@hello.com> posted :
I want to validate that the data entered by a user is correct before they
submit the form.
You can only do that if you know what it should be, and in that case you
don't need to bother with the form.
How do I validate the following (or what functions/classes do I use):

- Ensure a text field is not empty
- Ensure that a text field contains only numeric values
- Ensure that the contents of a text field is shorter than a certain length
- Remove commas and other formatting from a field before submission


For checks of compliance with a given format, which is not the same
thing as before, see <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of 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.
Sep 14 '05 #6
Davey <da***@hello.com> wrote in message news:43**********@x-privat.org...
I want to validate that the data entered by a user is correct before they
submit the form.

How do I validate the following (or what functions/classes do I use):
You have to invent your own and there are plenty of tutorials around.
Does your form look anything like this?

- Ensure a text field is not empty
Is this a homework assignment.? <input type=text name='hw'>

- Ensure that a text field contains only numeric values
How many days before it's due ? <input type=text name='late'>

- Ensure that the contents of a text field is shorter than a certain length
How do you rate your percentage chances of passing the course? <input type=text name='noChance'>

- Remove commas and other formatting from a field before submission


How much is this course worth to you? <input type=text name='bribe'>

--
S.C.
Sep 15 '05 #7
"Stephen Chalmers" <ig******@lycos.co.uk> wrote in message
news:43********@mk-nntp-2.news.uk.tiscali.com...
Davey <da***@hello.com> wrote in message news:43**********@x-privat.org...
I want to validate that the data entered by a user is correct before they
submit the form.

How do I validate the following (or what functions/classes do I use):


You have to invent your own and there are plenty of tutorials around.
Does your form look anything like this?

- Ensure a text field is not empty


Is this a homework assignment.? <input type=text name='hw'>

- Ensure that a text field contains only numeric values


How many days before it's due ? <input type=text name='late'>

- Ensure that the contents of a text field is shorter than a certain
length


How do you rate your percentage chances of passing the course? <input
type=text name='noChance'>

- Remove commas and other formatting from a field before submission


How much is this course worth to you? <input type=text name='bribe'>


No it looks like this.

Is Stephen Chalmers a wanker?
<select name='ChalmersWanker'>
<option value='1'>Yes</option>
<option value='2'>Definitely</option>
</select>
Sep 15 '05 #8
"Davey" schrieb:
I want to validate that the data entered by a user is correct before
they submit the form.


If there's a need to validate input for security reasons, do *not* do
this on the client side by using JavaScript. It's just too easy to get
around this. Of course there's nothing to say against an *additional*
client side check, but you shouldn't rely on that.

Greetings,
Jan
Sep 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Andrew Williams | last post: by
4 posts views Thread by EmmettPower | last post: by
5 posts views Thread by Steve | last post: by
reply views Thread by Gary Shell | last post: by
4 posts views Thread by Anthony P | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.