More questions about search function on regular expressions

Discussion in 'Javascript' started by .Net Sports, May 8, 2007.

  1. .Net Sports

    .Net Sports Guest

    I posed this problem last month here, but need more of a
    clarification, as I still cannot correctly flag a bad password. I need
    users to submit a passowrd that has at least one numeric character,
    along with any amount of string-alpha characters (8 numeric characters
    and 1 string character is fine, for example), but having trouble with
    the correct regex, as the below still doesnt raise the error alert
    when all strings with no numbers are entered:

    <script language="JavaScript" type="text/javascript">
    function validatePassword(password) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (password.value == "") {
    password.style.background = 'Yellow';
    error = "You didn't enter a password.\n";
    } else if ((password.value.length < 7) || (password.value.length >
    15)) {
    error = "The password is the wrong length. \n";
    password.style.background = 'Yellow';
    } else if (illegalChars.test(password.value)) {
    error = "The password contains illegal characters.\n";
    password.style.background = 'Yellow';
    } else if (!((password.value.search(/[A-Z]+[a-z]+[0-9]/)))){ // &&
    (password.value.search(/(0-9)+/)))) {
    error = "The password must contain at least one numeral.\n";
    password.style.background = 'Yellow';
    } else {
    password.style.background = 'White';
    }
    return error;
    }

    </script>

    Thanks in advance
    netsports
    .Net Sports, May 8, 2007
    #1
    1. Advertising

  2. .Net Sports

    Evertjan. Guest

    ..Net Sports wrote on 08 mei 2007 in comp.lang.javascript:

    > I posed this problem last month here, but need more of a
    > clarification, as I still cannot correctly flag a bad password. I need
    > users to submit a passowrd that has at least one numeric character,
    > along with any amount of string-alpha characters (8 numeric characters
    > and 1 string character is fine, for example), but having trouble with
    > the correct regex, as the below still doesnt raise the error alert
    > when all strings with no numbers are entered:
    >




    > <script type="text/javascript">


    do not use: language="JavaScript"

    > function validatePassword(password) {


    make sure password is the right object by using the "this" keyword

    > password.style.background = 'Yellow';


    yellow , no uppercase


    > if (!((password.value.search(/[A-Z]+[a-z]+[0-9]/)))){ // &&
    > (password.value.search(/(0-9)+/)))) {


    I don't understand these last two lines with // and two { {

    > password.style.background = 'White';


    white

    ================

    Try this, much simpler code:

    <script type="text/javascript">

    function validatePassword(password) {
    password.style.backgroundColor = 'yellow';
    if (password.value == '')
    return 'You didn't enter a password.\n';
    if ((password.value.length < 7) ||
    (password.value.length >15))
    return 'The password is of the wrong length.\n';
    if (/[\W_]/.test(password.value))
    return 'The password contains illegal characters.\n';
    if (/\d/.test(password.value))
    return 'The password must contain at least one numeral.\n';
    password.style.backgroundColor = 'white';
    return "";
    }

    </script>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 8, 2007
    #2
    1. Advertising

  3. .Net Sports

    Evertjan. Guest

    Evertjan. wrote on 09 mei 2007 in comp.lang.javascript:

    > Try this, much simpler code:
    >
    > <script type="text/javascript">
    >
    > function validatePassword(password) {
    > password.style.backgroundColor = 'yellow';
    > if (password.value == '')
    > return 'You didn't enter a password.\n';
    > if ((password.value.length < 7) ||
    > (password.value.length >15))
    > return 'The password is of the wrong length.\n';
    > if (/[\W_]/.test(password.value))
    > return 'The password contains illegal characters.\n';
    > if (/\d/.test(password.value))


    if (!/\d/.test(password.value))

    > return 'The password must contain at least one numeral.\n';
    > password.style.backgroundColor = 'white';
    > return "";
    >}
    >
    > </script>
    >




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 8, 2007
    #3
    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. Jay Douglas
    Replies:
    0
    Views:
    598
    Jay Douglas
    Aug 15, 2003
  2. Franz Steinhaeusler

    excluding search string in regular expressions

    Franz Steinhaeusler, Oct 21, 2004, in forum: Python
    Replies:
    9
    Views:
    1,148
    Franz Steinhaeusler
    Oct 22, 2004
  3. Victor Polukcht

    One more regular expressions question

    Victor Polukcht, Jan 18, 2007, in forum: Python
    Replies:
    9
    Views:
    337
    Neil Cerutti
    Jan 18, 2007
  4. Henning_Thornblad
    Replies:
    46
    Views:
    1,018
    Kris Kennaway
    Jul 10, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page