0

I have created a validate function using JavaScript. I need a validation that tests that password field in a form to make sure it is:

  1. At least 8 characters.
  2. Contains a numeric value.
  3. Contains an alphabetic value.

I just need an If statement inside my validate function

function Validate()
{
    with(document.memberInfo) {

        evt = new userInfo(username.value, password.value, email.value, firstname.value, lastname.value, age.value, description.value);
    }

    with(evt)
{
    if((email.indexOf("@",0)==-1))
    {
        alert("The email must contain the @ symbol.");
        return false;
    }






    evt.printEvent();
}
return true;
}
  • 1
    This isn't a free code writing service. You should show what you have tried, what you expected the result to be and what you got, including any errors. – RobG May 19 '16 at 06:13
  • I have updated the answer.. – nisar May 19 '16 at 06:31
  • There is still no code you've already created. – KittMedia May 19 '16 at 06:44
  • @RobG I have added my function and it works I just need help creating an if statement that validates the password section of my HTML form. My apologies I'm new to the site and I'm a programming student – Jasmin Kenjar May 19 '16 at 20:16

3 Answers3

0

you can use regex "/^(?=.*[0-9])(?=.*[a-zA-Z]).{8,}$/" refer this link stackoverflow

JsFiddle

var regex = /^(?=.*[0-9])(?=.*[a-zA-Z]).{8,}$/;

function getValue()  {
    return document.getElementById("myinput").value;
}

function test() {
    alert(regex.test(getValue()));
}

function match() {
    alert(getValue().match(regex));    
}

<input type="text" id="myinput" value="vexillology"/>
<button id="testBtn" onclick=test()>test</button>
<button id="matchBtn" onclick=match()>match</button>
Community
  • 1
  • 1
nisar
  • 1,055
  • 2
  • 11
  • 26
  • This restricts the password to only being alphanumeric though. – 4castle May 19 '16 at 06:16
  • That's not correct. That requires that there are at least 7 characters after the first group of alphanumeric characters. I think you just want `.{8,}` – 4castle May 19 '16 at 06:44
0

using regx function you can validate ur form . here is the code .

var xstr="^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$";
var str=Document.getElementById("id").value;
    var ck=xstr.exec(str);
    if(!ck || ck[0]!=str){
       //code
    }
Nihar Sarkar
  • 1,187
  • 1
  • 13
  • 26
0

Using regex is the way to go, but the more readable solution is probably:

function isValid(pass) {
    return pass.length >= 8 &&    // at least 8 characters
           /\d/.test(pass)  &&    // contains a digit
           /[A-Za-z]/.test(pass); // contains a letter
}

function isValid(pass) {
  return pass.length >= 8 &&
         /\d/.test(pass)  &&
         /[A-Za-z]/.test(pass);
}

var field = document.getElementById("password");
var output = document.getElementById("output");

field.onkeyup = function() {
  output.innerHTML = isValid(field.value) ? "Valid" : "Not Valid";
}
<input type="text" id="password" placeholder="Enter password" />
<span id="output"></span>

Alternatively, you can put it all in one regex:

function isValid(pass) {
    return /^(?=.*[A-Za-z])(?=.*\d).{8,}$/.test(pass);
}

JSFiddle

4castle
  • 32,613
  • 11
  • 69
  • 106