0

I have a problem. When I clicked the submit button nothing happens, even when I filled out the username and password with numbers (I don't want the username and password contains any number so I did make the condition for it), there is no alert display. I do not know where the problem comes from? Can you guys help me with this

Note: the reset function works fine

function validateInput() {
  var firstName = document.forms["sign_up"]["firstName"];
  var lastName = document.forms["sign_up"]["lastName"];
  var email = document.forms["sign_up"]["email"];
  var reg = /^[a-zA-Z]+$/;
  if (firstName.value !== '' || lastName.value !== '' || email.value !== '') {
    if (firstName.value.match(reg) && lastName.value.match(reg)) {
      alert("Form is submitted");
      // return true;
      return false; // for the demo, so it doesn't submit
    } else {
      if (firstName.value.match(reg) === false) {
        document.getElementById("error").innerHTML = "Numbers are not allowed in username";
        return false;
      } else if (lastName.value.match(reg) === false) {
        document.getElementById("error").innerHTML = "Numbers are not allowed in password";
        return false;
      }
    }
  }
}

function reset() {
  document.getElementById("first").innerHTML = "";
  document.getElementById("last").innerHTML = "";
  document.getElementById("email").innerHTML = "";
}
<form id="sign_up" onsubmit="return validateInput()">
  <p id="error"></p>
  <label for="firstName">First Name</label>
  <input type="text" id="firstName" value="" placeholder="Enter your first name">
  <label for="lastName">Last Name</label>
  <input type="text" id="lastName" value="" placeholder="Enter your last name">
  <label for="email">Email</label>
  <input type="email" id="email" value="" placeholder="Enter your email">
  <button type="submit">Submit</button>
  <button type="button" onclick="reset();">Cancel</button>
</form>
Phil
  • 157,677
  • 23
  • 242
  • 245
BeefNoodle
  • 123
  • 2
  • 13

3 Answers3

3

Use the Pattern attribute in input for validation like below

<input type="text" id="firstName" value="" pattern="[^0-9]*" title="Numbers are not allowed" placeholder="Enter your first name">

for more references: https://www.w3schools.com/tags/att_input_pattern.asp

And for reset functionality use reset

<input type="reset" value="reset">

It's better than create a special function for it and it saves your number of lines:-)

Rabby
  • 322
  • 4
  • 15
1

First, try to avoid to inline event handlers as they are not rec-emended at all. Also to reset form values you can simply use reset() method on the form.

Also, do not use innerHTML just to set the text of your error. You can use textContent instead which is better fit in your example.

You can use addEventListener with submit event to check for validation on your firstname and lastname.

I have fixed your code and its all working as expected.

Live Working Demo:

let form = document.getElementById("sign_up")
var firstName = document.getElementById("firstName")
var lastName = document.getElementById("lastName")
var email = document.getElementById("email")
var reset = document.getElementById("clearValues")
var reg = /^[a-zA-Z]+$/;

form.addEventListener('submit', function(e) {
  e.preventDefault()
  if (firstName.value != '' || lastName.value != '' || email.value != '') {
    if (firstName.value.match(reg) && lastName.value.match(reg)) {
      alert("Form is submitted");
    } else if (!firstName.value.match(reg)) {
      document.getElementById("error").textContent = "Numbers are not allowed in username";
    } else if (!lastName.value.match(reg)) {
      document.getElementById("error").textContent = "Numbers are not allowed in password";
    }
  }
})

reset.addEventListener('click', function(e) {
  document.getElementById("sign_up").reset();
})
input {
  display:block;
}
<head>
</head>
<body>
  <form id="sign_up" action="#">
    <p id="error"></p>
    <label for="firstName">First Name</label>
    <input type="text" id="firstName" value="" placeholder="Enter your first name">
    <label for="lastName">Last Name</label>
    <input type="text" id="lastName" value="" placeholder="Enter your last name">
    <label for="email">Email</label>
    <input type="email" id="email" value="" placeholder="Enter your email">
    <button type="submit">
       Submit
     </button>
    <button type="button" id="clearValues" onclick="reset();">
       Cancel
     </button>
  </form>
</body>
Always Helping
  • 14,316
  • 4
  • 13
  • 29
0

You don't need to return a function in onsubmit event. This should work fine.

 <form id="sign_up" onsubmit="validateInput()">

Reference: https://www.w3schools.com/jsref/event_onsubmit.asp

Dhruv
  • 149
  • 4
  • This is incorrect. See these two examples; [without return](https://jsfiddle.net/rjb3k4us/) vs [with return](https://jsfiddle.net/rjb3k4us/1/) – Phil Sep 21 '20 at 04:11