0

I am trying to validate form but whenever I click submit button it says:

ReferenceError: check_text is not defined

even though variable is present in local scope.

<form class="input-text-box" method="post" action="">
    <input type="text" id="uname" name="uname" placeholder="First Name" autocomplete="off" required>                        
    <input type="text" id="ulastname" name="ulastname" placeholder="Last Name" autocomplete="off" required>
    <input type="text" id="uuname" name="uuname" placeholder="Username" autocomplete="off" required>
    <input type="text" id="contact" name="contact" placeholder="Contact" autocomplete="off" required>
    <input type="email" id="email" name="uemail" placeholder="Email Address" autocomplete="off" required>
    <input type="password" id="pass-1" name="upass1" placeholder="Password" autocomplete="off" required>
    <input type="password" id="pass-2" name="upass2" placeholder="Confirm Password" autocomplete="off"required>
    <input type="submit" value="Sign Up" id="submit-btn" onclick="validate()">
     <p class="term-cond">By joining, you agree to our <a href="">Terms of Service</a> </p>
     <p class="term-cond" style="margin:40px 0 0 65px;">Already a member? <a href="login.html">Sign In</a> </p>
</form>

Below is the Javascript code which is intended to validate the values:

var msg="";
  var DOMstring = {
  firstName:'uname',
  lastName:'ulastname',
  userName:'uuname',
  contact:'contact',
  email:'email',
  pass1:'pass-1',
  pass2:'pass-2',
  submit:'submit-btn'
}
function validate(){
  var status = false;
  var firstName = document.getElementById(DOMstring.firstName).value;
  status = checkText(firstName,'First Name');
  var lastName = document.getElementById(DOMstring.lastName).value;
  status = checkText(lastName,'Last Name');
  var userName = document.getElementById(DOMstring.userName).value;
  status=checkText(userName,'Username');
  var contact = document.getElementById(DOMstring.contact).value;
  status = checkNumber(contact);
  var pass1 = document.getElementById(DOMstring.pass1).value;
  status = checkPass(pass1);
  var pass2 = document.getElementById(DOMstring.pass2).value;
  status = comparePass(pass1,pass2);

  if(status == true){
    alert("successfully created an account");
    return true;
  }else{
      alert("Please consider the following error messages<br>"+msg);
      return false;
   }
}

function checkText(DOMname,name){
  var ckeck_text =  /^[A-Za-z]$/;
  if(!check_text.test(DOMname)){
    msg += name + ' cannot contain numbers or special character.<br>';
    return false;
   }
 return true;
}

function checkNumber(DOMnumber){
  var check_number = /^[0-9]{10}$/;
  if(!check_number.test(DOMnumber)){
    msg += 'Number contains 10 digit only<br>';
    return false;
  }
  return true;
}

function checkPass(DOMpass1){
  var check_pass = /^(?=.*[\d])(?=.*[!@#$%^&*])[\w!@#$%^&*]{8,16}$/;
  if(!check_pass.test(DOMpass1)){
    msg += 'Password field should contain alphanumeric values and special character<br> and should be in range from 8 to 16<br>';
    return false;
  }
return true;
}

function comparePass(DOMpass1,DOMpass2){
  if(!DOMpass1 === DOMpass2){
    msg += 'Both password does not match<br>';
    return false;
  }
  return true;
}

I don't understand how there is no variable inside of same name if I had used let instead of var then this output is reasonable but with following method why it is not defined. If anything I am missing please help me to understand as I am completely new to Javascript.

1 Answer 1

1

You have a typo here var ckeck_text = /^[A-Za-z]$/;

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy.. you saved the day.. Do you think this code would work or not for field validation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.