0

Stated Javascript yesterday. I am writing client side form validation using JavaScript and am using a bunch of if statements for my code.

This is it so fa.:

function validateloginform() { //login page validation test //
    var username = document.forms["form"]["username"].value;
    var password = document.forms["form"]["password"].value;
    var verifypassword = document.forms["form"]["verifypassword"].value;
    if (document.form.username.value == null || document.form.username.value == "") {
        alert("Username is blank...it must be entered");
        document.form.username.focus();
        return false;
    }
    if (document.form.username.value.length != 8) {
        alert("Username must be 8 characters long");
        document.form.username.focus();
        return false;
    } else {
        alert("Correct");
        return true;
    }
}

How can I be more specific with my statements to include combination of numerical and alphabetical ([a-z] or [0-9]) characters.

If if the username or password do not contain a combination of letters and numbers return false. Also, is there any way to include special characters?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
user2857205
  • 3
  • 1
  • 4
  • possible duplicate of [Regular Expression for password validation](http://stackoverflow.com/questions/2370015/regular-expression-for-password-validation) – Qantas 94 Heavy Apr 26 '14 at 11:44
  • It's not a good idea to hand out this many clues on what user (and password should look like. Registering would be different, but personally I wouldn't deliver anything but something on the form is wrong. – Tony Hopkinson Apr 26 '14 at 11:45

1 Answers1

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Check out that link for a regular expression tutorial with Javascript. Here is an example (from the tutorial, but I added some explanations) if you want to cut to the chase.

<!DOCTYPE html>
<html>  
  <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
    <meta http-equiv="Content-Script-Type" content="text/javascript">  
    <script type="text/javascript">  
      // Create a regular expression
      // This regular expression is used to check the user's input phone number is in
      // a common phone number format.
      var re = /(?:\d{3}|\(\d{3}\))([-\/\.])\d{3}\1\d{4}/;  
      function testInfo(phoneInput){  
        // The exec() call below checks if the phoneInput.value matches the regex re
        // which was defined above.
        var OK = re.exec(phoneInput.value);  
        // OK will be true or false depending on if phoneInput.value matched the regex.
        if (!OK)  
          window.alert(RegExp.input + " isn't a phone number with area code!");  
        else
          window.alert("Thanks, your phone number is " + OK[0]);  
      }  
    </script>  
  </head>  
  <body>  
    <p>Enter your phone number (with area code) and then click "Check".
        <br>The expected format is like ###-###-####.</p>
    <form action="#">  
      <input id="phone"><button onclick="testInfo(document.getElementById('phone'));">Check</button>
    </form>  
  </body> 

Zack
  • 69
  • 1
  • 3
  • 11