I have a form which takes the user email and password. I have setup validation with express validator which checks if the user email and password are empty and if email is a valid email as shown below.
const loginSchema = [
body("email")
.isEmail()
.withMessage("email must contain a valid email address")
.notEmpty()
.withMessage("please enter email"),
body("password").notEmpty().withMessage("please enter password"),
];
When testing in postman, if a user was to submit the form without entering email and password, it displays all error messages. How do I use conditionals with express validator to make it so that isEmail() withMessage is only called if the request body email is not empty?
From express-validator docs:
If the option onlyFirstError is set to true, then only the first error for each field will be included.
So you basically have to do this instead
validationResult(req).array({ onlyFirstError: true })
Alternatively, you can use the following to have the errors from a request be a map from field path to first error of that field:
validationResult(req).mapped()
Related
I was working with express-validator when i encountered that i used normalize email for validation of email while signing up so i stored the normalized email to server.
Validation code:
router.post(
"/signup",
[
check("name").not().isEmpty(),
check("email").normalizeEmail().isEmail(),
check("password").isLength({ min: 6, max: 15 }),
],
userController.signupUser
);
Input email: abc.123#mail.com
normalized email: abc123#gmail.com (storing in db)
Now the problem is when working on login the input email doesn't matches and shows invalid credentials.
You just need to pass options inside normalizeEmail ():
normalizeEmail ({"gmail_remove_dots": false "})
After researching and observing I noticed that the code which was used at the time of signing up can be used to normalize email at the time of login, I just had to add:
router.post("/login",[check('email').normalizeEmail()], userController.loginUser);
After adding the email was converting to normalized one and can be used directly from requests.
You cannot remove any . or _ since it's part of the user's e-mail.
Here and example of validating an email address
I have a form called sub_form with 2 inputs, one of the inputs is an email address, the other is a choice from a dropdown menu. The Javascript below checks if the email address is valid, and also prints the values of both the inputs to the console log.
The problem is, when an incorrect email is entered, it shows the error message but still prints to the console.
How can I fix this so that the inputs only print to console if a valid email is entered?
<!-- validator -->
$('#sub_form').bootstrapValidator({live: 'disabled',
fields: {
email: {
validators: {
emailAddress: {
message: 'Invalid email' } }},}});
<!--print to console-->
document.getElementById('sub_form').onsubmit = function(){
console.log(document.getElementById('email').value);
console.log(document.getElementById('dropdown').value);
return false;
}
Per my comment on the question, this validation library as far as I've been able to tell is an older version of the one here. My company happens to use the same one, so I've been using that site a lot for reference. Just replace mentions of the "formValidation" method with "bootstrapValidator", and events use the "bv" namespace instead of "fv".
The validator should provide some methods for checking its valid state. You could use:
var validator = $('#sub_form').data('bootstrapValidator');
if (validator.isValidField('email')) {
console.log(document.getElementById('email').value);
}
See here for more info.
I have a website which uses contact form 7 for capturing user data, there is a email field which needs to be validated as well as verified as a valid user. i came across api which charge you for that.
My code for checking the validation is as follows
<!--Email must be a valid email -->
jQuery('#contact_email').on('input', function() {
var input=jQuery(this);
var re = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
var is_email=re.test(input.val());
if(is_email){input.removeClass("invalid").addClass("valid"); jQuery(".not-valid-email").css("visibility","hidden");}
else{input.removeClass("valid").addClass("invalid"); jQuery(".not-valid-email").css("visibility","visible");}
});
how can i also validate if the entered valid email address is also authentic ?
I want to make a Meteor app where users can only create an account if their email ends with #mydomain.com.
In the end, they would actually only need to enter their username and not the #mydomain.com part.
So, the create user field would look like:
Name: __________
eMail: __________#mydomain.com
Password: __________
Renter: __________
How would I go about doing this?
You use accounts package: meteor add accounts-password.
Then you would configure it in server-side code (http://docs.meteor.com/#accounts_config): Accounts.config({restrictCreationByEmailDomain:'mydomain.com'});
And then use Accounts.createUser in combination with custom UI that autofills the email domain part.
I'm assuming you're using Meteor's built-in accounts management packages. To limit signups to mydomain.com email addresses, put the following in server-side code:
Accounts.validateNewUser(function(user) {
if (/#mydomain\.com$/.test(user.emails[0].address.toLowerCase())) {
return true;
} else {
throw new Meteor.Error(403, "Email domain not allowed.");
}
});
As for helping them with adding the #mydomain.com, write some client-side code that validates the field in the login form where they enter their username. If it lacks an #, tack #mydomain.com onto the end of it before the form gets submitted.
I need to validate user input. The user can enter a full to header for an email message, so it may look like any of the following:
user#example.com
<user#example.com>
User <user#example.com>
"User" <user#example.com>
user#example.com, User2 <user2#example.com>
..etc. All of the above are valid To fields.
However, when user input is something like this:
user#example.com, not_an_email, User <user.example.com>
Then I want to respond with an error for the last 2 emails.
It's not a problem for me to check a plain email address for validity, but the extra String and braces <> I am a bit clueless about at the moment.