Password contains one number

Discussion in 'JavaScript' started by adamjblakey, Feb 8, 2011.

  1. #1
    Hi,

    I have the following running which works fine:

    
     if (form.password.value == "") {
        alert( "Please enter a password" );
        form.password.focus();
        return false ;
      }
    
    if (form.password.length < 6) {
        alert( "Password must be greater than 6 characters" );
        form.password.focus();
    	form.password.value = "";
        return false ;
      }  
    
    Code (markup):
    I want to add into this a check to see if the user has entered at least one number. I have tried various things which have not worked. Can anyone please advise.

    Cheers,
    Adam
     
    adamjblakey, Feb 8, 2011 IP
  2. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try something like this

    if (document.forms["formname"]["password"].value.length < "6")
    {
    alert( "Password must be greater than 6 characters" );
    return false;
    }
     
    srisen2, Feb 8, 2011 IP
  3. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There are many solutions, this would be one of them:
    
    if (form.password.value.search(/[0-9]+/) == -1) {
        alert("bla bla");
        form.password.focus();
    	return false;
    }
    
    Code (markup):
     
    hogan_h, Feb 8, 2011 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Thanks a lot hogan_h
     
    adamjblakey, Feb 9, 2011 IP