Re: JavaScript to validate User input



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
.



Relevant Pages

  • Re: Get text "literally" from a TextBox
    ... Cor and Patrice, thanks for the answer; I know the regular expressions, but ... my problem is how get the pattern string if the user put that in a Textbox. ... maybe I can depure my string, but exist another especial "characters" like ...
    (microsoft.public.dotnet.languages.vb)
  • Re: insert into/update table in db with long text
    ... If you need to store more characters than the ... database has an new empty row instead of the string which should be there. ... mytext is type of "text". ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: [variant coding] Fonts question
    ... >> The buffer must be large enough to store the path and file name string or strings ... I cannot for the life of me fathom why this morning I thought that was related to the amount of available characters. ...
    (rec.games.roguelike.angband)
  • Re: Small confusion about negative lookbehind
    ... > My candidate string is "ab". ... > The expressions I'm testing this string against are the following, ... but the position between characters. ... Regular expressions describe not only strings, ...
    (comp.lang.java.programmer)
  • Re: regex/replace white list
    ... It removes the problem that Regular Expressions ... cannot span lines because string concatenation serves the purpose. ... RegExp special characters parsed as expression atoms instead. ... The latter introduces the maintenance problem that the ...
    (comp.lang.javascript)