JavaScript to validate User input

Discussion in 'Javascript' started by Amit, Nov 12, 2007.

  1. Amit

    Amit Guest

    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
     
    Amit, Nov 12, 2007
    #1
    1. Advertising

  2. Amit

    Erwin Moller Guest

    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
     
    Erwin Moller, Nov 12, 2007
    #2
    1. Advertising

  3. Amit

    Amit Guest

    On Nov 12, 5:55 pm, (Doug Miller) wrote:
    > In article <>, 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

    >
    > 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
     
    Amit, Nov 12, 2007
    #3
  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
     
    Bart Van der Donck, Nov 12, 2007
    #4
  5. Amit

    Doug Miller Guest

    In article <>, 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.
     
    Doug Miller, Nov 12, 2007
    #5
  6. 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.
     
    Dr J R Stockton, Nov 12, 2007
    #6
  7. Amit

    Amit Guest

    On Nov 13, 1:13 am, Dr J R Stockton <> wrote:
    > 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. ? 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
     
    Amit, Nov 13, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    704
    Walter Roberson
    Sep 9, 2005
  2. Replies:
    3
    Views:
    419
  3. Santosh

    Validate User Input before calling java script function

    Santosh, Jun 3, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    167
    Scott M.
    Jun 5, 2006
  4. phal
    Replies:
    12
    Views:
    241
  5. Abhishek
    Replies:
    5
    Views:
    236
    David Mark
    Aug 7, 2008
Loading...

Share This Page