Re: JavaScript to validate User input
- From: Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@xxxxxxxxxxxxxxxx>
- Date: Mon, 12 Nov 2007 13:57:29 +0100
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
.
- References:
- JavaScript to validate User input
- From: Amit
- JavaScript to validate User input
- Prev by Date: Re: JavaScript to validate User input
- Next by Date: Re: Your opinion about complex Javascript applications and performance
- Previous by thread: Re: JavaScript to validate User input
- Next by thread: Re: JavaScript to validate User input
- Index(es):
Relevant Pages
|