| SitePoint Sponsor |


Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript
I agree, checking whether the e-mail exists via AJAX is a good thing to do. The more you can validate for the user up front before submitting the form, the better. Just be sure to do all the same checks after the form is submitted, too.
I was thinking you'd want to do the password match check in JavaScript (you could do most of these checks in JavaScript, except for checking that the e-mail address hasn't been taken already and any other similar tasks) but PHP via AJAX works too. If you ever do use JavaScript for client-side validation though, whatever server-side script the form is being submitted to must also make the same validations since JavaScript can be bypassed.
Anyway, if you want to do the check server-side, the PHP code you wrote will work, except you need to change the calls to $_POST to $_GET since your AJAX calls use GET as their type. In validateIt, you'll also need to check if it's a password you're validating, in which case you need to pass the other password so that they can be checked to see if they match. You'll have to tweak your PHP code to allow for this on the server-side so that it can handle taking both passwords. And instead of calling GET directly in your function, you could just handle that up front by extracting the two password values right away and then passing them to the validateCheckPassword function, making it more reusable.
Hello again
I want to extend the validation on the "alphanum" method to accept spaces as well. The reason is that when a user enters their Postcode it can have spaces, so if i enter this:
It will return as invalid as there is a space. So i thought of changing the method to this:OL45 1DF
But that didn't work it still returned as "invalid"?PHP Code:function validateAlphanum($value) {
if(preg_match("[^A-Za-z0-9\s]", $value)) {
echo "true";
} else {
echo "invalid";
}
}
Any ideas what i can do for this?
Many thanks,
Software Engineer ASP, ASP.NET, VB, PHP
Kind regards
Billy


Please do not change the behaviour of the validateAlphaNum function, because it will have unexpected consequences when other parts of the code use it, and spaces really are not allowed in those other parts.
What you can do instead, is to not use alphanum for the postcode, but to use a special "postcode" one, from which you can remove spaces from the value that is sent to the validateAlphaNum function. That's the simplest solution.
Code php:case 'postcode': validateAlphanum(str_replace(' ', '', $value)); break;
Alternatively you can create a completely different function that performs the precise post code checks that you need.
Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript
Brilliant, thats worked for me
Also, if i want to enforce a minimum length for a number such as the phone number, i was trying to use this:
But that didn't work. Reason i am doing this is that a person can enter a phone number of a single digit and that is not correct.^\d{5}$
Can you see what i am doing wrong above?
Many thanks,
EDIT - I got it to work with this: /^[0-9]{6,}$/
Software Engineer ASP, ASP.NET, VB, PHP
Kind regards
Billy
Bookmarks