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

Validating form fields

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a

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

Phal

Sep 14 '05 #3

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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.