I need to have a field that will not allow data to be inputted if it returns a message of “false”. The field “ponumber” checks against a DB and if that record already exists I dont want it to allow the user to use that particular PO Number. Basically it blanks it out if they leave the field and displays the message. The script is working perfect except now allowing the input based on the false return from checkponumberajax php file.
$(document).ready(function () {
var validateponumber = $('#validateponumber');
$('#ponumber').keyup(function () {
var t = this;
if (this.value != this.lastValue) {
if (this.timer) clearTimeout(this.timer);
validateponumber.removeClass('error').html('<img src="/images/ajax-loader.gif" height="16" width="16" /> checking availability...');
this.timer = setTimeout(function () {
$.ajax({
url: 'includes/checkponumberajax.php',
data: 'action=check_ponumber&ponumber=' + t.value,
dataType: 'json',
type: 'post',
success: function (j) {
validateponumber.html(j.msg);
}
});
}, 200);
this.lastValue = this.value;
}
});
});
<input type="text" name="ponumber" value="<?=@$_REQUEST['ponumber']?>" id="ponumber" />
and my checkponumber.php file returns like this
if ($records != 0) {
$response = array(
'ok' => false,
'msg' => "The selected PO# is not available");
} else {
$response = array(
'ok' => true,
'msg' => "This PO# is free");
}
Edit: Solved!
I ended up thanks to @bourch using this which simply blanks out the field once a value is reached that matches false
if(j.ok != true){$("#ponumber").attr("value", "");}
Try this: