473,606 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript to validate User input

Dear Friends

I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'

Characters other than these will not be allowed

If a user enters characters other than the mentioned above , the alert
message should be displayed

typical steps would be

1 declare variables of integer types

var li_len, li_idx, li_value
2 declar variables of string types
var ls_tmp_string, ls_char

3 Remove the white spaces from as_payment_code string and store it in
ls_tmp_string

4 Calulate the length of the string variable ls_tmp_string and store
it in li_len
li_len = ls_tmp_string.l ength;

5 For li_idx = 1 to li_len

get the substring and store it in ls_char

var ls_char = ls_tmp_string.s ubstr(li_idx,1) ;

Check ls_char for 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop
'.',Comma ',',Plus '+'

if it cotains any of the above values then continue

else
show a pop-up and display message ( "Error!", "Invalid character: "
+ ls_char ) and return false

Thank you

Nov 12 '07 #1
7 2232
In article <11************ **********@d55g 2000hsg.googleg roups.com>, Amit <Am*********@gm ail.comwrote:
>Dear Friends

I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'

Characters other than these will not be allowed

If a user enters characters other than the mentioned above , the alert
message should be displayed

typical steps would be

1 declare variables of integer types

var li_len, li_idx, li_value
2 declar variables of string types
var ls_tmp_string, ls_char

3 Remove the white spaces from as_payment_code string and store it in
ls_tmp_strin g

4 Calulate the length of the string variable ls_tmp_string and store
it in li_len
li_len = ls_tmp_string.l ength;

5 For li_idx = 1 to li_len

get the substring and store it in ls_char

var ls_char = ls_tmp_string.s ubstr(li_idx,1) ;

Check ls_char for 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop
'.',Comma ',',Plus '+'

if it cotains any of the above values then continue

else
show a pop-up and display message ( "Error!", "Invalid character: "
+ ls_char ) and return false
Do your own damn homework. Having other people do it for you might enable you
to pass the class, but you'll never learn anything that way.
>
Thank you
You're welcome.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
Nov 12 '07 #2
Amit wrote:
Dear Friends

I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'

Characters other than these will not be allowed

If a user enters characters other than the mentioned above , the alert
message should be displayed

typical steps would be

1 declare variables of integer types

var li_len, li_idx, li_value
2 declar variables of string types
var ls_tmp_string, ls_char

3 Remove the white spaces from as_payment_code string and store it in
ls_tmp_string

4 Calulate the length of the string variable ls_tmp_string and store
it in li_len
li_len = ls_tmp_string.l ength;

5 For li_idx = 1 to li_len

get the substring and store it in ls_char

var ls_char = ls_tmp_string.s ubstr(li_idx,1) ;

Check ls_char for 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop
'.',Comma ',',Plus '+'

if it cotains any of the above values then continue

else
show a pop-up and display message ( "Error!", "Invalid character: "
+ ls_char ) and return false

Thank you
Hi,

I would make a regular expression for this type of datamatching.
If you are unfamiliar with regular expression, you'll have to do all
this the old fashoined way, substringing, positionmatchin g, walking over
arrays, etc.
That tends to be a lot of (errorprone) work.

Hence regular expressions were invented. :-)

You'll find a lot of good resources on the web. Also a lot of bad ones
for that matter. ;-)

Or buy the great book 'Mastering regular expressions' by O'Reilly.

Also, read on here:
www.jibbering.com/faq/
and find the part that deals with regular expressions.
But that site is dead at the moment (at least from here, via XS4ALL in
the netherlands, Europe)

Good luck.

Regards,
Erwin Moller
Nov 12 '07 #3
On Nov 12, 5:55 pm, spamb...@milmac .com (Doug Miller) wrote:
In article <1194870940.489 931.136...@d55g 2000hsg.googleg roups.com>, Amit <Amit.Bas...@gm ail.comwrote:


Dear Friends
I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed
If a user enters characters other than the mentioned above , the alert
message should be displayed
typical steps would be
1 declare variables of integer types
var li_len, li_idx, li_value
2 declar variables of string types
var ls_tmp_string, ls_char
3 Remove the white spaces from as_payment_code string and store it in
ls_tmp_string
4 Calulate the length of the string variable ls_tmp_string and store
it in li_len
li_len = ls_tmp_string.l ength;
5 For li_idx = 1 to li_len
get the substring and store it in ls_char
var ls_char = ls_tmp_string.s ubstr(li_idx,1) ;
Check ls_char for 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop
'.',Comma ',',Plus '+'
if it cotains any of the above values then continue
else
show a pop-up and display message ( "Error!", "Invalid character: "
+ ls_char ) and return false

Do your own damn homework. Having other people do it for you might enable you
to pass the class, but you'll never learn anything that way.
Thank you

You're welcome.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.- Hide quoted text -

- Show quoted text -
Hi Doug

Its not a home work
In our JSP page the data validations happen on Click on OK button
We are doing the reverse engineering of already developed project in
PB
The validation code written in PB and now I have to write it in Java
Script
I dont have much exposure to Java Script as Im a C++ resource

Nov 12 '07 #4
Amit wrote:
I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed
var str = '09azAZ -.,+';
if (!(/^[\da-z\s-\.,\+]*$/i.test(str))) alert('not OK');

Hope this helps,

--
Bart

Nov 12 '07 #5
In article <11************ **********@o80g 2000hse.googleg roups.com>, Amit <Am*********@gm ail.comwrote:
>I dont have much exposure to Java Script as Im a C++ resource
In that case... tell your client to hire someone else who knows JavaScript.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
Nov 12 '07 #6
In comp.lang.javas cript message <11************ *********@o38g2 000hse.goo
glegroups.com>, Mon, 12 Nov 2007 05:51:32, Bart Van der Donck
<ba**@nijlen.co mposted:
>Amit wrote:
>I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed

var str = '09azAZ -.,+';
if (!(/^[\da-z\s-\.,\+]*$/i.test(str))) alert('not OK');
That tests the whole string to see that every character is legitimate.
But there is no need to proceed past the first illegitimate character.

if (/[^\da-z\s-\.,\+]/i.test(str)) alert('not OK') // equivalent

And ISTM that \s should be replaced by a space, as tab is not allowed;
and that the - which stands for itself should be last (or first, or
escaped), and that the + need not be escaped. The OP should check each
character in the RegExp against the manual, and for having exactly the
desired effect.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk DOS 3.3, 6.20 ; WinXP.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demo n.co.uk/batfiles.htm- also batprogs.htm.
Nov 12 '07 #7
On Nov 13, 1:13 am, Dr J R Stockton <j...@merlyn.de mon.co.ukwrote:
In comp.lang.javas cript message <1194875492.763 453.67...@o38g2 000hse.goo
glegroups.com>, Mon, 12 Nov 2007 05:51:32, Bart Van der Donck
<b...@nijlen.co mposted:
Amit wrote:
I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed
var str = '09azAZ -.,+';
if (!(/^[\da-z\s-\.,\+]*$/i.test(str))) alert('not OK');

That tests the whole string to see that every character is legitimate.
But there is no need to proceed past the first illegitimate character.

if (/[^\da-z\s-\.,\+]/i.test(str)) alert('not OK') // equivalent

And ISTM that \s should be replaced by a space, as tab is not allowed;
and that the - which stands for itself should be last (or first, or
escaped), and that the + need not be escaped. The OP should check each
character in the RegExp against the manual, and for having exactly the
desired effect.

--
(c) John Stockton, Surrey, UK. ?...@merlyn.dem on.co.uk DOS 3.3, 6.20 ; WinXP.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demo n.co.uk/batfiles.htm- also batprogs.htm.

Thanks John

that was useful

Doug , thanks for your valuable comment

Nov 13 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13383
by: Don W. | last post by:
Is it possible to open a browser window from a javascript for the purpose of gathering user input on a form and then use data from the form in the javascript? How do you get the data from the form into the script? Don W.
2
1439
by: GIMME | last post by:
Background ... I've created a web application that allows a user to create an HTML application from IE. The application itself creates an XML representation of a XHTML form. The XHTML representation can be saved as a string and recreated. (The application also has a crude workflow aspect - so XMHTML forms can be created and assigned a workflow. Forget I said anything about
4
9462
by: Madha K | last post by:
I am developing a web application that need to support UTF-8 characters. For client side validations, can javascript be used to handle UTF-8 characters. 1) How javascript can be used to restrict non-utf8 characters? 2) Using javascript how to find the lengh of a string having unicode characters? e.g: For a field Name on the form, there is a corrosponding field Name varchar2(10) in DB. Through my application when i try enter 10 normal...
19
6871
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the JavaScript in this code from a couple different sites but I'm not 100% sure what each line is doing...This is the ASP code that I'm using for the page....Take a look at the JavaScript code and please let me know what each line is doing....I have been...
4
1685
by: JJ_377 | last post by:
Why won't Netscape do the intended in the following? IE is able to render this page as intended. It is is a "self-posting" ASP form with a JavaScript validation function: if the form fails the JavaScript validation function, then the form should not post and an alert should be issued as indicated below. The *first* alert in the Validate function works in NS, but the rest of the code does not and therefore I wonder about the way I am...
1
1349
by: amerar | last post by:
Hi All, Here is my situation: I have a form where the user can perform a number of tasks. One of these tasks is to enter a new record. The form has an 'onsubmit' property to validate the data if the users enters a new record. However, there is also a button to allow the user to 'duplicate' a stored record. In that case, I do not want to perform the entire validation since most of the data will be fetched from the database
7
5150
by: dredge | last post by:
Hi Everyone, I have been asked to build a PHP application that calculates important financial information based on some user-inputted numbers and that will not allow the user to continue forward unless a certain percentage range has been met. To validate the numbers, I am considering using Javascript as opposed to having the PHP code validate the numbers because Javascript is faster (it is almost instantaneous because the validation...
27
4719
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...
3
2089
by: anthonybrough | last post by:
I have an asp page that has a form to collect user data in a form. when the user clicks submit the input is validated. If any fields are not acceptable the user clicks on a button to go back to the original form to correct the input. This all works fine until I try to incorporate a javascript to display a popup calendar which posts the selected date back to a field on the form. This script works fine in itself, however if the page is...
19
2212
by: maya | last post by:
hi, so what is "modern" javascript?? the same as "DOM-scripting"? i.e., editing content (or changing appearance of content) dynamically by massaging javascript objects, html elements, etc? (in conjunction with css, you know, the usual...;) this is what is meant by "modern" javascript?? so how do folks feel about this who think javascript is so evil they disable it from their browsers?? do sites designed with "modern" javascript...
0
8010
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
7942
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8433
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
8429
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...
0
8300
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6761
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
5461
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
3969
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2443
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

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.