JavaScript to validate User input

A

Amit

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.length;

5 For li_idx = 1 to li_len

get the substring and store it in ls_char

var ls_char = ls_tmp_string.substr(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
 
E

Erwin Moller

Amit said:
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.length;

5 For li_idx = 1 to li_len

get the substring and store it in ls_char

var ls_char = ls_tmp_string.substr(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, positionmatching, 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
 
A

Amit

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.


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
 
B

Bart Van der Donck

Amit said:
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,
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Mon, 12 Nov 2007 05:51:32, Bart Van der Donck
Amit said:
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.
 
A

Amit

In comp.lang.javascript message <[email protected]
glegroups.com>, Mon, 12 Nov 2007 05:51:32, Bart Van der Donck
Amit wrote:
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. [email protected] DOS 3.3, 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.


Thanks John

that was useful

Doug , thanks for your valuable comment
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top