Last week I asked a question about live username availability. Paul Wilkins gave me some useful links which I have working now, But at the same time I still need to check if no fields are empty when the form is submitted and I am realy struggling to make this work. I have been looking for tutorials and/or examples of a combination of those two (Form Validation and Live email check) but couldn’t find any. So what I basically need is a function for three fields (name, username and password). None of the fields should be empty and the email field should be checked against values from the database!
Does anyone know of a good example or tutorial where those 2 functionalities are combined.
Hi Pullo. Thank you fot thr reaction The fields involved are: Name, Profile name, username, password, city and Province. I realy hope you can help me out. This is driving me insane over the last couple of days. I have tried a lot of things but nothing was working as I had hoped for. Thank you in advance
jQuery’s validation plugin makes it easy for such validation to occur. You can also add custom validation methods, so that the ajax request about checking the email can be done too.
We’re wanting something extra though. When someone has entered their email, we want to do a special check to see if that email already exists, We can use the PHP code to check if the email already exists:
Yes, using the inline attribute is a better option when dealing with required, but I’ve found that it can get tricky to explain the details of which types of validation terms are accepted as inline tag attributes, when those details differ from the set documentation.
Given that though, the following is a preferred way to deal with things, where required is in the HTML code:
Hi Paul and Pullo. Thank you both for your input. Still this is the live email check, where I need the email check in combination with the normal validation. And that is the point where I get stucked. Ok I know I can use this rule:
Indeed - the form tag has an action attribute, which contains the PHP file that the form gets submitted to.
Do not just blindly assume that the form contents are going to be good though. There are several reasons for why scripting won’t end up checking the form, so always validate the form’s contents from the server side too.
The rule of thumb is to always validate from the server-side, and to use client-side validation where practical to provide a better user experience.
Hi Paul and Pullo. I am nearly there with the Validation. Thank you both for the input, but I am a bit struggling with the remote file (remote: ‘check-email.php’) This is what I have in the function:
rules: {
email: {
required: true,
email: true,
remote: "modules/site/check-email.php"
}
}
messages: {
email: {
required: "Vul uw email adres in.",
email: "Vul een geldig email adres in.",
remote: jQuery.validator.format("{0} is al in gebruik.")
}
}
But no mather what email address I type in, I get the message that it is already in use. Even with email adresses that are not in the database. What am I doing wrong?
Good, now use an ajax command to check a non-existing email. If you get “true” back as an answer, how long is that string of data? If JavaScript says that it’s longer than 4 characters, then some PHP work needs to be done.
[ed: looking at the documentation takes us on a different track, see below]
I also see from the documentation that a GET request is made to the server when the check is made. That might go a long way towards helping to solve things.
I also see in the documentation that “The response is evaluated as JSON and must be true for valid elements”, so it looks like the output from the PHP file needs to be as JSON data:
Hi Paul. Sorry for maybe a stupid question But what kind of ajax command and where and how? I know a little bit of java but this is completely new to me
Edit: Just to test it I left it on GET and now it is working it seems