[英]How to use the username availability checker script in form validation jquery
I am new to jquery. 我是jquery新手。 has i got the following code to check the username availability.
我有以下代码来检查用户名是否可用。
The script is working fine. 该脚本运行正常。 to check the username avialble or not.
检查用户名是否有效。
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(function()
{
$('.user_name').keyup(function()
{
var checkname=$(this).val();
var availname=remove_whitespaces(checkname);
if(availname!=''){
$('.check').show();
$('.check').fadeIn(400).html('<img src="image/ajax-loading.gif" /> ');
var String = 'username='+ availname;
$.ajax({
type: "POST",
url: "available.php",
data: String,
cache: false,
success: function(result){
var result=remove_whitespaces(result);
if(result==''){
$('.check').html('<img src="image/accept.png" /> This Username Is Avaliable');
$(".check").removeClass("red");
$('.check').addClass("green");
$(".user_name").removeClass("yellow");
$(".user_name").addClass("white");
}else{
$('.check').html('<img src="image/error.png" /> This Username Is Already Taken');
$(".check").removeClass("green");
$('.check').addClass("red")
$(".user_name").removeClass("white");
$(".user_name").addClass("yellow");
}
}
});
}else{
$('.check').html('');
}
});
});
function remove_whitespaces(str){
var str=str.replace(/^\s+|\s+$/,'');
return str;
}
available.php contains following code. available.php包含以下代码。
if(isset($_POST['username']) && !empty($_POST['username'])){
$username=mysql_real_escape_string($_POST['username']);
$query="select * from sell where LOWER(uname)='$username'";
$res=mysql_query($query);
$count=mysql_num_rows($res);
if($count > 0){
echo "true";
}else{
echo "false";
}
}
Everything works fine. 一切正常。 the ajax posting and checking the value exists or not.
ajax发布并检查值是否存在。
But my problem is How i can include the above script in the following jquery validation script. 但是我的问题是我如何将上述脚本包含在以下jquery验证脚本中。
$(document).ready(function(){
$("#f2").validate({
debug: false,
rules: {
name: {
required:true,
minlength:3
//Here how to call the above script function..i stuck here..
}
});
});
Based on the name availability i need to process the form to submit.php other wise the form won't be submitted.. 基于名称的可用性,我需要处理表单Submit.php,否则将不提交表单。
Any suggestions,Acceptable. 任何建议,可以接受。
Add this script in validation function 在验证功能中添加此脚本
<script>
$(document).ready(function(){
$("#f2").validate({
debug: false,
rules: {
name: {
required:true,
minlength:3,
remote:{
url: "available.php",
type: "post",
data: {
username: function() {
return $( ".user_name" ).val();
}
}
}
}
}
});
</script>
Remove "keyup" event function and than you are done... 删除“ keyup”事件功能,然后就完成了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.