Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This category lists all questions and answers related to the Javascript programming language.
How to validate email address in JavaScript?
Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium) [code]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,}Read more
Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)
[code]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(String(email).toLowerCase());[/code]
}
Here’s the example of regular expresion that accepts unicode:
[code]var re = /^(([^<>()[].,;:s@”]+(.[^<>()[].,;:s@”]+)*)|(“.+”))@(([^<>()[].,;:s@”]+.)+[^<>()[].,;:s@”]{2,})$/i;[/code]
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
[code]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);
}[/code]
[code]
function validate() {
var $result = $(“#result”);
var email = $(“#email”).val();
$result.text(“”);
if (validateEmail(email)) {
$result.text(email + ” is valid :)”);
$result.css(“color”, “green”);
} else {
$result.text(email + ” is not valid :(“);
$result.css(“color”, “red”);
}
return false;
}
$(“#validate”).bind(“click”, validate);[/code]
[code]<script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script>
<form>
<p>Enter an email address:</p>
<input id=’email’>
<button type=’submit’ id=’validate’>Validate!</button>
</form>
<h2 id=’result’></h2>[/code]
See lessHow to to prevent other event handlers from executing after a certain event is fired ?
return false from within a jQuery event handler is effectively the same as calling both e.preventDefault and e.stopPropagation on the passed jQuery.Event object.e.preventDefault() will prevent the default event from occuring, e.stopPropagation() will prevent the event from bubbling up and return falRead more
return false from within a jQuery event handler is effectively the same as calling both e.preventDefault and e.stopPropagation on the passed jQuery.Event object.
e.preventDefault() will prevent the default event from occuring, e.stopPropagation() will prevent the event from bubbling up and return false will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up
Source: John Resig
See lessHow do i redirect user from page to another in JavaScript ?
Hi Saly, Actually you can achieve this by simulate HTTP redirect by using one of the following : // similar behavior as an HTTP redirect [code]window.location.replace("http://www.oraask.com");[/code] or // similar behavior as clicking on a link [code]window.location.href("http://www.oraask.com");[/cRead more
Hi Saly,
Actually you can achieve this by simulate HTTP redirect by using one of the following :
// similar behavior as an HTTP redirect
[code]window.location.replace(“http://www.oraask.com”);[/code]
or
// similar behavior as clicking on a link
[code]window.location.href(“http://www.oraask.com”);[/code]
at the end this your choice but for my suggestion i prefer using (window.location.replace) because this doesn’t keep originate page in the session history.
hope this help.