JQuery Validation Plugin Remote Validation Error always fires [duplicate] - javascript

This question already has answers here:
jQuery Validate remote method usage to check if username already exists
(2 answers)
Closed 8 years ago.
On my home page I have a log in modal that asks for the users email address and password through a basic form. When the email address is entered I have a remote validation that checks to see if the email address is in my blacklisted table in my DB. But the remote error fires off no matter if the PHP returns false or true and will not let the form submit, even if the PHP returns true. All of the validation works correct except for the remote. I can not seem to fix this problem and I have looked all over and nothing I find can solve this problem.
Home jQuery
$(function(){
$("#frmSignIn").validate({
rules: {
InputEmailAddress: {
required: true,
email: true,
remote:"scripts/blacklist.php"
},
InputPassword: {
required: true,
minlength: 3,
maxlength: 20
}
},
messages: {
InputEmailAddress:{
required:"Don't forget your email address!",
email:"Please enter a valid email address",
remote:"This email address has been backlisted"
},
InputPassword:{
required:"Please enter your password",
minlength:"Password must be longer than 3 characters",
maxlength:"Password can not be longet than 20 characters"
}
},
submitHandler: function(){
var data = $("#frmSignIn").serialize();
$.post('posted.php', data, function(o){
console.log(o);
},'json');
}
});
});
PHP
I will add the actual table check after I get this working
$email = $_GET['InputEmailAddress'];
$valid = true;
echo json_encode($valid);

The problem was that remote:'scripts/blacklist.php' was in single ''
Soon as I changed it to double quotes "scripts/blacklist.php" it works fine...
If anyone knows why I would love to know so I can understand, but problem solved!

Related

How to normalize the input email?

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

Only show the first issue during Semantic UI form validation

In my Chrome Extension I have a form with an email field which displays two different messages during validation (one error for an empty field, another for an invalid format).
The problem is, using the following validator my form displays both issues. I want to stop validating as soon as any field has an issue and display only the first error message.
fields: {
email: {
identifier: 'email',
rules: [
{
type: 'empty',
prompt: 'Please enter your email address'
},
{
type: 'email',
prompt: 'Please enter a valid email address'
}
]
}
}
At the moment I work around the issue with the following css hack, but I'd rather just finish the validation so it doesn't try to validate the rest of the form.
.ui.form .ui.error.message .list:not(.ui) li:not(:first-child) {
display: none;
}
How do I stop validation after the first failure with Semantic UI?

How do I improve this email address validation method?

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.

backbone validate single attribute in save only

I have one model with 2 attributes. Let me explain first.
Backbone.Model.extend({
validation: {
username: [
{required: true, msg: 'Enter email/username.'},
],
password: [
{required: true,msg: 'Enter password.'},
{minLength:20,msg: 'Incorrect password length.'}]
},
});
I want validate single attribute in save function. Do you have any idea?
means, If my username & password field is blank, then error should be occurred for username only.
I am using backbone.validation with backbone.
Thanks
To validate a single or multiple properties, using Backbone.Validation, use the following code:
yourModel.isValid('username') // or
yourModel.isValid([ 'attribute1', 'attribute2' ])
There are two approaches you might use:
Approach 1
I think the easiest way to do this would be that you don't set the password field until after the username is validated. To quote from the FAQ:
What gets validated when?
If you are using Backbone v0.9.1 or later, all attributes in a model will be validated. However, if for instance name never has been set (either explicitly or with a default value) that attribute will not be validated before it gets set.
This is very useful when validating forms as they are populated, since you don't want to alert the user about errors in input not yet entered.
If you need to validate entire model (both attributes that has been set or not) you can call validate() or isValid(true) on the model.
So, don't call validate on your whole model. Call it on the username field first, then the password field.
Also, don't set the password field in your model until after the username has been validated.
Approach 2
Another approach would be to use the conditional validation described in the FAQ:
Do you support conditional validation?
Yes, well, sort of. You can have conditional validation by specifying the required validator as a function.
So, your code might look something like:
Backbone.Model.extend({
validation: {
username: [
{required: true, msg: 'Enter email/username.'},
],
password: [
{required: function(val, attr, username) {
return Bool(username); //some code here- return true if username is set, false if it is not. This rough example may not work in the real world.
},msg: 'Enter password.'},
{minLength:20,msg: 'Incorrect password length.'}]
},
});
I'm pretty sure that's what Ulugbek Komilovich is suggesting, though I'm not sure the syntax in that answer is quite correct.
M = Backbone.Model.extend({
validation: {
username: [
{required: true, msg: 'Enter email/username.'},
],
password: function(value, attr, computedState) {
// new M(this.get('username')); //or use second way
if(this.get('username')) {
return 'Enter email/username.';
}
// other checks
}
},
});

Troubleshooting remote jquery plugin requests

I'm trying to use the remote option to check an email and see if the entered one is currently in the database. Using the Remember the Milk example on the site I have my js function set up as follows:
$(function() {
// validate signup form on keyup and submit
$("#registrationForm").validate({
rules: {
firstname: "required",
lastname: "required",
password: {
required: true,
minlength: 5
},
password_confirm: {
required: true,
minlength: 5,
equalTo: "#password"
},
email: {
required: true,
email: true,
remote: "byob/processors/checkEmail.php"
}
}
});
});
However nothing is happening. I thought that perhaps I had the path to the file wrong, but looking under the Network tab in the Chrome Inspector.. no call is being made to the php file; I expect that one should be, and I should at least be getting a 404.
If this document would not show up that way, please advise on some methods to try troubleshooting this, since without getting a response from the server it is very difficult!
Seems to work just fine for me..
Keep in mind that the validation for existence will happen after it passes the required (must be non-empty) check and the valid email check..
Make sure the input is named correctly as well..
demo at http://jsfiddle.net/gaby/XFpcN/1/

Categories