I have what used to be a working form validation, including the remote check of a username as available. we've added a lot of other javascript to the script.js file and at some point recently the remote part of this broke. There is only one form field being checked by validate, the new_name field. It is required (works) and must be available (doesn't work).
Here is the jQuery:
$('#nickname_form').validate({
rules: {
new_name: {
required: true,
remote: {
url: '/api/screenname_unique/',
type: 'post'
}
}
},
messages: {
new_name: {
required: 'Please choose a Forum Username.',
remote: 'That Username is already taken or contains invalid characters.'
}
}
});
As I said the above used to work, with no changes. I've checked the rest of the script.js file and no errors are showing up. Also, in the site, we're not seeing JS errors anywhere. If I remove the remote portion of the above code, the required check works and the form will submit when there is a value in the field.
With remote in place, the form will not submit, and whether the ajax response from the remote call is true or false, there is no jQuery validate error message shown. Here is the page called by remote. It works fine as far as the response it is giving for a given value:
<?php
header('Content-type: application/json');
//get the post value
$screen_name = $_POST['new_name'];
//get their member_id
$member_id = $this->EE->session->userdata['member_id'];
//return false if no screen_name provided
if((!$screen_name) || (!$member_id)) {
echo json_encode(false);
exit;
} else { //there is a screen_name
//Regex Check for valid chars
$valid = !preg_match('/[^a-z0-9_ -]/i',trim($screen_name));
if (!$valid) {
echo json_encode(false);
exit;
}
//SQL
$results = $this->EE->db->query("SELECT member_id FROM exp_members WHERE screen_name = '$screen_name' and member_id <> '$member_id' limit 1");
if ($results->num_rows() > 0) {
echo json_encode(false);
} else {
echo json_encode(true);
}
}
?>
I am not really sure where to go from here. Ideas?
I was using JQ 1.5.1 and just updated to 1.6.3 and now the form is working fine. I guess maybe there was an issue with remote and 1.5.1? Thanks guys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With