Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > JavaScript to validate User input

Reply
Thread Tools

JavaScript to validate User input

 
 
Amit
Guest
Posts: n/a
 
      11-12-2007
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

 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      11-12-2007
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
 
Reply With Quote
 
 
 
 
Amit
Guest
Posts: n/a
 
      11-12-2007
On Nov 12, 5:55 pm, spamb...@milmac.com (Doug Miller) wrote:
> In article <1194870940.489931.136...@d55g2000hsg.googlegroups .com>, Amit <Amit.Bas...@gmail.com> 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

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

 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      11-12-2007
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

 
Reply With Quote
 
Doug Miller
Guest
Posts: n/a
 
      11-12-2007
In article < .com>, Amit <> wrote:

>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.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      11-12-2007
In comp.lang.javascript message <
glegroups.com>, Mon, 12 Nov 2007 05:51:32, Bart Van der Donck
<> posted:
>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.
 
Reply With Quote
 
Amit
Guest
Posts: n/a
 
      11-13-2007
On Nov 13, 1:13 am, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote:
> 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.com> posted:
>
> >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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript to Validate the Phone Number Field Using Javascript :-) Abhishek Javascript 5 08-07-2008 09:14 AM
Validate User Input before calling java script function Santosh ASP .Net Datagrid Control 1 06-05-2006 12:08 AM
Perl output Javascript to validate user input phal Perl Misc 12 04-10-2006 10:09 PM
Getting User Input after getting Input from a file dei3cmix@uga.edu C++ 3 03-23-2006 05:01 AM
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)? santa19992000@yahoo.com C Programming 4 09-09-2005 03:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57