473,387 Members | 1,535 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,387 software developers and data experts.

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 4368
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gleep | last post by:
Hi PHP coders, I've got an issue I'm stuck with. Imagine there is a large form that has 5 columns and 20 rows. In each row there is a check box - then 4 input fields. I already have the code...
4
by: Andrew Williams | last post by:
Does anyone know of a way to quickly validate an HTML web form? I need to make sure that certain fields on my form actually contain data and are not submitted blank. I have tried using...
1
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there...
4
by: EmmettPower | last post by:
Hi, I have a form which includes a field 'number'. When 'number' is changed additional fields ('item_0', etc) are generated on the form using 'onchange'. I want to validate the form using...
3
by: Anonymous | last post by:
Hi all, I have a form people use to enter checking data. One of the fields is calculated based on finding the difference of two input fields on the form. Here are the fields: CheckAmount...
5
by: Steve | last post by:
I am currently trying to validate data in an access database. I need to verify that columns containing date information are in the format ddmmyyyy and columns containg time information are in the...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
16
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event doesn't fire if you open a dialog box in button 1's...
4
by: Anthony P | last post by:
Hello Everyone, I am having some trouble getting my JavaScript to validate my form fields. I've included more information below. My explaination of the code is in ALL CAPS so it stands out from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...

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.