469,600 Members | 2,543 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

Nov 12 '07 #1
7 2008
In article <11**********************@d55g2000hsg.googlegroups .com>, Amit <Am*********@gmail.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.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
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.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
Nov 12 '07 #3
On Nov 12, 5:55 pm, spamb...@milmac.com (Doug Miller) wrote:
In article <1194870940.489931.136...@d55g2000hsg.googlegroups .com>, Amit <Amit.Bas...@gmail.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.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

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**********************@o80g2000hse.googlegroups .com>, Amit <Am*********@gmail.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.javascript message <11*********************@o38g2000hse.goo
glegroups.com>, Mon, 12 Nov 2007 05:51:32, Bart Van der Donck
<ba**@nijlen.composted:
>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.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.
Nov 12 '07 #7
On Nov 13, 1:13 am, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
In comp.lang.javascript message <1194875492.763453.67...@o38g2000hse.goo
glegroups.com>, Mon, 12 Nov 2007 05:51:32, Bart Van der Donck
<b...@nijlen.composted:
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.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

Nov 13 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Don W. | last post: by
4 posts views Thread by Madha K | last post: by
19 posts views Thread by dmiller23462 | last post: by
1 post views Thread by amerar | last post: by
7 posts views Thread by dredge | last post: by
27 posts views Thread by Chris | last post: by
3 posts views Thread by anthonybrough | last post: by
19 posts views Thread by maya | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.