0

I am using JavaScript to validate email. The problem is, when the email ids don't match, then one alert button will come. Once I click the button it still takes me to the other page, instead of same page to correct my mail id.

HTML:

<label for="department">Email ID</label>
<input type="email" size="30" name="email" id="email" required />
<label for="department">Confirm Email ID</label>
<input type="email" size="30" name="cname" id="confirm_email" required />
<input type="submit" name="submit" value="submit" class="button" onClick="validate()">

JavaScript:

function validate()
{
 if(document.getElementById("email").value != document.getElementById("confirm_email").value)
 alert("Email do no match");
}
Cerbrus
73.3k19 gold badges138 silver badges151 bronze badges
asked Feb 9, 2015 at 9:18
1
  • 3
    try adding a return false when the email doesn't match for blocking the form submit Commented Feb 9, 2015 at 9:21

7 Answers 7

7

You need to tell the submit button to not perform the submit

function validate()
{
 if (document.getElementById("email").value!=document.getElementById("confirm_email").value) {
 alert("Email do no match");
 return false;
 }
}
answered Feb 9, 2015 at 9:22
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is because You have taken button type=submit

Change input type='button'

<input type="button" name="submit" value="submit" class="button" onClick="validate()">

and submit form using javascript

document.getElementById("myForm").submit();

I case you want to validate only on submit then use

 event.preventDefault();

and then validate but after successful validation you have to submit the form using js or jq. JS method is given above and jq method is:

$("form").submit();
answered Feb 9, 2015 at 9:24

1 Comment

i need it in my form submit only . while submitting only it must be validate
1

You should add return false; in your if code block if you dont want the redirect.

Its the browser's default to refresh the page when the form is submitted. To prevent this refresh, add return false;.

Learn more: return | MDN

answered Feb 9, 2015 at 9:21

6 Comments

Why the downvotes? If you are downvoting, please have the courtesy to mention WHY.
I don't really see how the use of window.location.href is justified here, as ekans suggested, blocking the form submit is most likely what the OP was looking for.
@Olivier Even my code will block the form submit, PLUS, it will redirect.
@Olivier Requoting what OP said: once i click the button it will takes me to the other page not to same page to correct my mail id. So when the user clicks OK on the alert, it should redirect.
@Rahul Desai nowhere within that text does the OP state that he actually wishes for a redirect. It's more like he states that it's an error within his issue.
|
0
<html>
 <head>
 <script>
 function validate(){
 if(document.getElementById("email").value != document.getElementById("confirm_email").value){
 alert("Email do no match");
 return false;
 }
 }
 </script>
 </head>
 <body>
 <form action="formsubmit.php" method="post" onsubmit="return validate()">
 <label for="department">Email ID</label>
 <input type="email" size="30" name="email" id="email" required />
 <label for="department">Confirm Email ID</label>
 <input type="email" size="30" name="cname" id="confirm_email" required />
 <input type="submit" name="submit" value="submit" class="button">
 </form>
 </body>
</html>
answered Feb 9, 2015 at 9:33

Comments

0

Use the below javascript code, your html code is correct!

Well executing the JavaScript code in StackOverflow Script Runner won't run and occur erorrs. If input boxes with email and confirm_email id(s) are declared, this should work.

Hope it could help!

function validate(){
 if(!document.querySelector("#email").value === document.querySelector("#confirm_email").value){
 alert("Email do not match.");
 }
}
/* In JavaScript, the ! keyword before the condition belongs to execute the statement if the given condition is false. */

answered Feb 27, 2022 at 16:19

Comments

-1

It must prevent the form to get submitted if the validation is failed. so

 return validate();

must be there. So if the validate function returns a false value then it will stop the form to be submitted. If the validate function return true then the submission will be done.

 <form method='post' action='action.php'> 
 <label for="department">Email ID</label>
 <input type="email" size="30" name="email" id="email" required />
 <label for="department">Confirm Email ID</label>
 <input type="email" size="30" name="cname" id="confirm_email" required />
 <input type="submit" name="submit" value="submit" class="button" onClick="return validate();"> 
 </form>
<script>
 function validateEmail(email) { 
 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
 return re.test(email);
 } 
 function validate(){
 if(!validateEmail(document.getElementById('email').value))
 {
 alert('Please enter a valid email');
 email.focus();
 return false;
 }
 else if(document.getElementById('email').value!=document.getElementById('confirm_email').value) {
 alert('Email Mismatch');
 confirm_email.focus();
 return false;
 }
 return true;
 }
 </script> 
answered Feb 9, 2015 at 9:26

Comments

-1

Fix that and remove type=submit and use a function or use following code:

<script>
function check(){
//* Also add a id "submit" to submit button*//
document.querySelector("#submit").addEventListener("click", function(){
//* Perform your actions when that submit button will be clicked and close with this in next line*//
})</script>
answered Jul 16, 2021 at 18:15

1 Comment

Your code is not syntactically correct, say WHY you are added an ID, no need for<script> tag as the format indicates it is code

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.