The problem appears to be a few things.
1) You're selecting the email input with an id selector, so add the proper id.
<input id='em' type='email' placeholder="Email" name='em'>
This will allow your blur to actually get triggered.
Now, you will get an error in the console. myEmail is not defined, as it isn't. Go ahead and assign it to the value of the email input
$('#em').blur(function () {
myEmail = $('this').val();
2) Rather than applying invalid and valid to a div, you'll probably want to add them to a list item as you did in the username.
<div id=email_info class=info>
<h4> The email you typed is incorrect: </h4>
<ul>
<li id=eformat class=invalid>
A valid email format looks like: username@example.com
</li>
</ul>
</div>
And then apply the fading in and out as you did before
if (isValidEmail(myEmail)){
$('#email_info').fadeOut('slow');
} else {
$('#email_info').fadeIn('slow');
}
This will fix email. See your updated Gist here
Password Confirmation
As for password confirmation, you need to add the same id you're selecting again.
<td><input id='pa2' type='password' placeholder='Repeat password' name=pa2></input>
Since you are selecting it with pa2 here
$('#pa2').blur(function () {
Also, you'll need to define pswd along with the already defined pswd2.
var pswd = $('#pa1').val();
var pswd2 = $('#pa2').val();
This should fix both problems. See updated gist here
Answer from ruevaughn on Stack OverflowThe problem appears to be a few things.
1) You're selecting the email input with an id selector, so add the proper id.
<input id='em' type='email' placeholder="Email" name='em'>
This will allow your blur to actually get triggered.
Now, you will get an error in the console. myEmail is not defined, as it isn't. Go ahead and assign it to the value of the email input
$('#em').blur(function () {
myEmail = $('this').val();
2) Rather than applying invalid and valid to a div, you'll probably want to add them to a list item as you did in the username.
<div id=email_info class=info>
<h4> The email you typed is incorrect: </h4>
<ul>
<li id=eformat class=invalid>
A valid email format looks like: username@example.com
</li>
</ul>
</div>
And then apply the fading in and out as you did before
if (isValidEmail(myEmail)){
$('#email_info').fadeOut('slow');
} else {
$('#email_info').fadeIn('slow');
}
This will fix email. See your updated Gist here
Password Confirmation
As for password confirmation, you need to add the same id you're selecting again.
<td><input id='pa2' type='password' placeholder='Repeat password' name=pa2></input>
Since you are selecting it with pa2 here
$('#pa2').blur(function () {
Also, you'll need to define pswd along with the already defined pswd2.
var pswd = $('#pa1').val();
var pswd2 = $('#pa2').val();
This should fix both problems. See updated gist here
Isn' t there a typo here?
if( !isValidEmail(myEmail) ){
$('#remail').removeClass('valid').addClass('invalid');
}
else{
$('#remail').removeClass('valid').addClass('invalid');
}
The both lines are equal!
Remove the required: true rule.
Demo: Fiddle
jQuery('.validatedForm').validate({
rules: {
password: {
minlength: 5,
},
password_confirm: {
minlength: 5,
equalTo: "#password"
}
}
});
Just a quick chime in here to hopefully help others... Especially with the newer version (since this is 2 years old)...
Instead of having some static fields defined in JS, you can also use the data-rule-* attributes. You can use built-in rules as well as custom rules.
See http://jqueryvalidation.org/documentation/#link-list-of-built-in-validation-methods for built-in rules.
Example:
<p><label>Email: <input type="text" name="email" id="email" data-rule-email="true" required></label></p>
<p><label>Confirm Email: <input type="text" name="email" id="email_confirm" data-rule-email="true" data-rule-equalTo="#email" required></label></p>
Note the data-rule-* attributes.
Few issues we have in your current implementation:
a. The error you're likely getting is that password is undefined.
Right now you're doing:
var y=document.forms["myForm"]["password"].value;
but you refer to it as "password" further on:
var uppercase = password.match(/[A-Z]/)
var lowercase = password.match(/[a-z]/g)
change the var y to:
var password=document.forms["myForm"]["password"].value;
b. To validate email, you should use a Regex such as:
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,}))$/;
var isValidEmail = re.test(email);
c. To check for the password rules, you should just rely on the regular expressions you have in place already (and strip out the atpos, dotpos usage - that makes it much more complicated than it even needs to be).
Example:
var email='[email protected]';
var password='test-P1assword';
var hasUpper = password.match(/[A-Z]/)
var hasLower = password.match(/[a-z]/g)
var hasNumber = password.match(/[0-9]/g)
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,}))$/;
var isValidEmail = re.test(email);
if (isValidEmail && hasUpper && hasLower && hasNumber) {
alert("Valid Email Address and Password");
return true;
} else {
alert("Not a valid e-mail address or password");
return false;
}
JSFiddle example, complete with Regex to validate email AND password: http://jsfiddle.net/4hH3T/2/
The regex was taken from: Validate email address in JavaScript?
you can use regex to validate.
var reg=/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/;
var isValid=reg.test(inputemail);
You will need to divide the logic in your pwcheck into separate validator methods. Below is a prototype of the same:
var value = $("#password_reg").val();
$.validator.addMethod("checklower", function(value) {
return /[a-z]/.test(value);
});
$.validator.addMethod("checkupper", function(value) {
return /[A-Z]/.test(value);
});
$.validator.addMethod("checkdigit", function(value) {
return /[0-9]/.test(value);
});
$.validator.addMethod("pwcheck", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);
});
$('#signup-form').validate({
rules: {
password: {
minlength: 6,
maxlength: 30,
required: true,
//pwcheck: true,
checklower: true,
checkupper: true,
checkdigit: true
},
confirmPassword: {
equalTo: "#passwd_reg",
},
},
messages: {
password: {
pwcheck: "Password is not strong enough",
checklower: "Need atleast 1 lowercase alphabet",
checkupper: "Need atleast 1 uppercase alphabet",
checkdigit: "Need atleast 1 digit"
}
},
highlight: function(element) {
var id_attr = "#" + $(element).attr("id") + "1";
$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
$(id_attr).removeClass('glyphicon-ok').addClass('glyphicon-remove');
$('.form-group').css('margin-bottom', '5px');
$('.form').css('padding', '30px 40px');
$('.tab-group').css('margin', '0 0 25px 0');
$('.help-block').css('display', '');
},
unhighlight: function(element) {
var id_attr = "#" + $(element).attr("id") + "1";
$(element).closest('.form-group').removeClass('has-error').addClass('has-success');
$(id_attr).removeClass('glyphicon-remove').addClass('glyphicon-ok');
$('#confirmPassword').attr('disabled', false);
},
errorElement: 'span',
errorClass: 'validate_cus',
errorPlacement: function(error, element) {
x = element.length;
if (element.length) {
error.insertAfter(element);
} else {
error.insertAfter(element);
}
}
});
.has-feedback .form-control-feedback {
top: 33px;
}
.validate_cus {
color: #a94442;
font-size: small;
}
label {
display: inline-block;
margin-bottom: 5px;
font-weight: 700;
}
.top-row > div {
float: left;
width: 48%;
margin-right: 4%;
}
.field-wrap {
position: relative;
margin-bottom: 20px;
}
input,
textarea {
font-size: 18px;
display: block;
height: 100%;
width: 100%;
padding: 5px 10px;
background: none;
background-image: none;
border: 1px solid #a0b3b0;
color: #545f58;
border-radius: 6px;
-webkit-transition: border-color .25s ease, box-shadow .25s ease;
transition: border-color .25s ease, box-shadow .25s ease;
}
input:disabled {
background: #eee;
}
.button:hover,
.button:focus {
background: #0b9444;
}
.button-block {
display: block;
width: 50%;
}
.button {
border: 0;
outline: none;
border-radius: 20px;
padding: 15px 0;
font-size: 1.6rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: .1em;
background: #187143;
color: #ffffff;
-webkit-transition: all 0.5s ease;
transition: all 0.5s ease;
-webkit-appearance: none;
}
#signup-form {
padding: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<form id="signup-form" action="login" method="post">
<div class="top-row">
<div class="form-group has-feedback field-wrap">
<label id="lbl_paswd" class="control-label" for="password">
Password
<span class="req">*
</span>
</label>
<input type="password" name="password" id="password_reg" class="" required autocomplete="off" />
<span class="glyphicon form-control-feedback" id="password_reg1">
</span>
</div>
<div class="form-group has-feedback field-wrap">
<label class="control-label" for="confirmPassword">
Confirm Password
<span class="req">*
</span>
</label>
<input type="password" name="confirmPassword" id="confirmPassword" class="" disabled required autocomplete="off" />
<span class="glyphicon form-control-feedback" id="confirmPassword1">
</span>
</div>
</div>
<button type="submit" class="button button-block">SIGN UP
</button>
</form>
code:
$.validator.addMethod("strongePassword", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);
},"The password must contain at least 1 number, at least 1 lower case letter, and at least 1 upper case letter");
Uses to :
rules: {
password: {
required: !0,
strongePassword: true
},
}
This worked for me.
Edited Answer:
To validate whether an email address is of the [email protected] format you can use the following regular expression:
var emailExp = new RegExp(/^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i);
This regular expression accepts only email addresses that contain an @-sign, a dot and a 2-4 characters long TLD.
You can use the above regular expression to validate a given email address as shown below:
function validate_email (email) {
/* Define the recommended regular expression. */
var emailExp = new RegExp(/^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i);
/* Test the email given against the expression and return the result. */
return emailExp.test(email);
}
jQuery Validator:
jQuery Validator doesn't support the use of regular expressions and instead uses the default HTML5 email regular expression internally, so you must first create a new method for the validator that does that:
$.validator.addMethod(
/* The value you can use inside the email object in the validator. */
"regex",
/* The function that tests a given string against a given regEx. */
function(value, element, regexp) {
/* Check if the value is truthy (avoid null.constructor) & if it's not a RegEx. (Edited: regex --> regexp)*/
if (regexp && regexp.constructor != RegExp) {
/* Create a new regular expression using the regex argument. */
regexp = new RegExp(regexp);
}
/* Check whether the argument is global and, if so set its last index to 0. */
else if (regexp.global) regexp.lastIndex = 0;
/* Return whether the element is optional or the result of the validation. */
return this.optional(element) || regexp.test(value);
}
);
Now that a method supporting validation against a regular expression was created for the validator, you can use the jQuery.validate as follows:
$('#element_id').validate({
email: {
required: true,
email: true,
regex: /^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i
}
});
Original Answer (Improved):
To filter an email address and only accept a format like [email protected] use this regular expression:
var emailExp = new RegExp(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i);
This regular expression filters out any "junk" that may be entered and requires that an @-sign, a dot and a 2-4 characters long TLD be present. If a substring of the given email address matches it the substring is returned, otherwise false.
You can use the above regular expression to filter a given email address as shown below:
function filter_email (email) {
var
/* Define the recommended regular expression. */
emailExp = new RegExp(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i),
/* Use 'match' to filter the email address given and cache the result. */
filtered = email.match(emailExp);
/* Return the filtered value or false. */
return filtered ? filtered[0] : false;
}
Notes:
When answering the OP's question more than a year ago, I mistook his intention for email validation as an attempt to filter a given string keeping only a substring of it that is an email address.
This answer considers addresses lacking a TLD invalid even though they are perfectly valid in the real world as per OP's request.
try this
function isEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
Use regular expersion to verify the email adddress:
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var email = $("#email").val();
var pwordLen = $("#pword").val().length;
if (regex.test(email) && pwordLen > 5) {
$('#btnSubmit').animate({ opacity: 1.0 }, 200);
}
else {
$('#btnSubmit').animate({ opacity: 0.0 }, 200);
}
regex.test(email) will return true if it is valid email id else return false.
To answer your question directly, you can check if certain characters exist like this:
var email = $("email").val();
var emailLen = email.length;
// important bit:
var hasAt = email.indexOf( "@" ) != -1;
var hasDot = email.indexOf( "." ) != -1;
or you can go a step further and check that at least one . is after the last @
var atPos = email.lastIndexOf( "@" );
var hasAt = atPos != -1;
var hasDot = email.indexOf( ".", atPos ) != -1;
The answer given by OSSCube is the more usual way of checking email addresses, but you should beware of unusual emails (such as those using IP addresses, quotes, or escaped characters). I believe that this method (simply checking [anything]@[anything].[anything]) will never have a false negative (shout up in the comments if I'm wrong!)