JavaScript Why does this form get submitted even if the required fields are left empty? how does this form a rainbow,does this ion form,

I am working with a form in which i take the password input from the user and then javascript checks if the passwords match. But even if the password is required, the form gets submitted if the input box is left empty. I am using javascript to submit the form. Although i have a solution to my problem but i wanted to ask that why this is happening and not the normal action of Please fill in this field, which normally appears when doing so.

HTML-

<form action="check.php" method="POST" id="userform">
USERNAME:<input name="uname" type="text" required/>
PASSWORD:<input type="password" id="upass" name="upass" type="text" required/>
RETYPE PASSWORD:<input type="password" id="reupass" name="reupass" type="text" required/>
<input type="button" value="Submit" onclick="passCheck()">
</form>

JavaScript-

function passCheck(){
var pass1 = document.getElementById('upass').value;
var pass2 = document.getElementById('reupass').value;
if(pass1 == pass2){
    document.getElementById('userform').submit();
}
else{
    alert("Both password inputs do not match. Please retry.");
    document.getElementById('userform').reset();
}
}

EDIT- I want that this should appear when i click on the button and the password fields are left empty:http://i.stack.imgur.com/cGuCK.jpg

Answer:1

Here is working JsFiddle Demo. try this code:

if(pass1 !="" && pass1 != pass2){
    alert("Both password inputs do not match. Please retry.");
    document.getElementById('userform').reset();
    return false;
}
else{
   document.getElementById('userform').submit();
}

Edit

if you change input type to submit it will automatically check null constraint and still you can validate fields

Here is Demo with type SUBMIT

Answer:2

<span class="classname"> <img src="" width="70" height="70"> </span> I'm trying to add an anchor tag with a URL around the tags. I'm not sure how to do it and I've been stumped for ...

<span class="classname"> <img src="" width="70" height="70"> </span> I'm trying to add an anchor tag with a URL around the tags. I'm not sure how to do it and I've been stumped for ...

I am creating an ajax slider with only the one "slide" thats data reloads during slide. My current method is to animate the absolute positioned element out of parent viewport, jump to right hand side ...

I am creating an ajax slider with only the one "slide" thats data reloads during slide. My current method is to animate the absolute positioned element out of parent viewport, jump to right hand side ...

What tag is counted in form.elements? <html> <head> <title> form process </title> <script> function showelements() { ...

What tag is counted in form.elements? <html> <head> <title> form process </title> <script> function showelements() { ...

I am trying to load data in JSONStore using loadData method, but it is only loading single record in store. Here is code snippet var myRecord = Ext.data.Record.create([ { name: 'Rid', ...

I am trying to load data in JSONStore using loadData method, but it is only loading single record in store. Here is code snippet var myRecord = Ext.data.Record.create([ { name: 'Rid', ...

  1. extjs jsonstore load data
  2. extjs store loaddata
  3. extjs store loaddata callback
  4. extjs store loaddata not working
  5. extjs store loaddata example
  6. extjs store loaddata event