0

Hello, I have an html5 form, I want to validate it and when clicking on submit, it calls the function register.
The way i done it below is not working. Can you please help me? I think i should do it with javascript but i dont know exactly.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Required Demo</title>
</head>
<body>

    <form>
        <label>
            First Name:
            <input required title="First Name is Required!" />
        </label>
        <label>
            Last Name:
            <input required title="Last Name is Required!" />
        </label>
       <input type="submit" value="Submit" onclick= "register()"/>
    </form>

</body>
</html>
4
Contributors
9
Replies
10
Views
4 Years
Discussion Span
Last Post by imran18
0

Try this instead...on the form element. Then place your javascript validation code in the validateForm function.

<form action="process.htm" onsubmit="return validateForm()" method="post">

0

actually i insert this ,

<form onsubmit="register()">

and it works. Can you tell me how can i validate the password field? the confirm password and password should be the same.

0

If all that you are concerend with is that the two password match, just inlcude a conditional statement in your code that checks if the value of the two input fields are equal.

0

The validation is done using JavaScript. Here is an example.

<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<style>
</style>

</head>

<body>

<form onsubmit="return validate(this);">
  <label>Password:
    <input type="password" name="password" value="" />
  </label>
  <label>Confirm password:
    <input type="password" name="confirm-password" value="" />
  </label>
  <input type="submit" name="submit" value="submit" />
</form>

<script>
function validate(form) {
  var e = form.elements;

  if(e['password'].value != e['confirm-password'].value) {
    alert('Your passwords do not match.');
    return false;
    } else {
    alert('Passwords matched!');
    return true;
    }
}
</script>
</body>
</html>
0

It looks like you don't even know what HTML5 and JavaScript mean... They are two separated topics. Please read the links I provided for you for more understanding.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.