ajax 用户名验证,php页面怎么写
html代码
<form id="iform" name="iform" method="post" action="__URL__/add">
<label><span>用户名:</span></label>
<input id="username" name="username1" class=":ajax;/mrthink.php" type="text" title="4-20位,注册成功后不可修改"/>
</form>
js代码
/*
*本插件原作者Vanadium,原文请移步前往http://vanadiumjs.com/查看
*转载及使用请务必注明原作者.
*/
/*$(function(){
//必填项加红*,Mr.Think添加,原插件无
// $("input[class*=:required]").after("<span> *</span>")
});*/
//弹出信息样式设置
Vanadium.config = {
valid_class: 'rightformcss',//验证正确时表单样式
invalid_class: 'failformcss',//验证失败时该表单样式
message_value_class: 'msgvaluecss',//这个样式是弹出信息中调用值的样式
advice_class: 'failmsg',//验证失败时文字信息的样式
prefix: ':',
separator: ';',
reset_defer_timeout: 100
}
//验证类型及弹出信息设置
Vanadium.Type = function(className, validationFunction, error_message, message, init) {
this.initialize(className, validationFunction, error_message, message, init);
};
Vanadium.Type.prototype = {
initialize: function(className, validationFunction, error_message, message, init) {
this.className = className;
this.message = message;
this.error_message = error_message;
this.validationFunction = validationFunction;
this.init = init;
},
test: function(value) {
return this.validationFunction.call(this, value);
},
validMessage: function() {
return this.message;
},
invalidMessage: function() {
return this.error_message;
},
toString: function() {
return "className:" + this.className + " message:" + this.message + " error_message:" + this.error_message
},
init: function(parameter) {
if (this.init) {
this.init(parameter);
}
}
};
Vanadium.setupValidatorTypes = function() {
Vanadium.addValidatorType('empty', function(v) {
return ((v == null) || (v.length == 0));
});
//***************************************以下为验证方法,使用时可仅保留用到的判断
Vanadium.addValidatorTypes([
//邮箱是否为空
['ajax1', function(v) {
return !Vanadium.validators_types['empty'].test(v);
}, '用户名必填!'],
['required', function(v) {
return !Vanadium.validators_types['empty'].test(v);
}, '您还没有输入个人或公司电子邮箱!'],
//电话是否为空
['dh1', function(v) {
return !Vanadium.validators_types['empty'].test(v);
}, '您还没有输入个人或公司电话号码!'],
['kong', function(v) {
return !Vanadium.validators_types['empty'].test(v);
}, '注册人/公司名称不能留空!'],
//强制选中
['accept', function(v, _p, e) {
return e.element.checked;
}, '必须接受!'],
//邮箱验证
['email', function (v) {
return (Vanadium.validators_types['empty'].test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v))
}, '邮箱格式不正确,例如:myname@sohu.com'],
//电话验证
['dh', function (v) {
return (Vanadium.validators_types['empty'].test(v) || /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/.test(v))
}, '电话号码格式不正确,例如:020-54678913'],
//
['min_length',
function (v, p) {
if (p === undefined) {
return true
} else {
return v.length >= parseInt(p)
}
;
},
function (_v, p) {
//alert(p);
return '输入字符长度不低于' + p + ''
}
],
['max_length',
function (v, p) {
if (p === undefined) {
return true
} else {
return v.length <= parseInt(p)
}
;
},
function (_v, p) {
return '输入字符长度不大于' + p + ''
}
],
//判断密码是相同
['same_as',
function (v, p) {
if (p === undefined) {
return true
} else {
var exemplar = document.getElementById(p);
if (exemplar)
return v == exemplar.value;
else
return false;
}
;
},
function (_v, p) {
var exemplar = document.getElementById(p);
if (exemplar)
return '两次密码输入不相同.';
else
return '没有可参考值!'
},
"",
function(validation_instance) {
var exemplar = document.getElementById(validation_instance.param);
if (exemplar){
jQuery(exemplar).bind('validate', function(){
jQuery(validation_instance.element).trigger('validate');
});
}
}
],
//ajax判断是否存在值
['ajax',
function (v, p, validation_instance, decoration_context, decoration_callback) {
if (Vanadium.validators_types['empty'].test(v)) return true;
if (decoration_context && decoration_callback) {
jQuery.getJSON(p, {value: v, id: validation_instance.element.id}, function(data) {
decoration_callback.apply(decoration_context, [[data], true]);
});
}
return true;
}]
,
//正则匹配,此用法不甚理解
['format',
function(v, p) {
var params = p.match(/^\/(((\\\/)|[^\/])*)\/(((\\\/)|[^\/])*)$/);
if (params.length == 7) {
var pattern = params[1];
var attributes = params[4];
try
{
var exp = new RegExp(pattern, attributes);
return exp.test(v);
}
catch(err)
{
return false
}
} else {
return false
}
},
function (_v, p) {
var params = p.split('/');
if (params.length == 3 && params[0] == "") {
return '输入的值必须与 <span class="' + Vanadium.config.message_value_class + '">' + p.toString() + '</span> 相匹配.';
} else {
return '提供的值与<span class="' + Vanadium.config.message_value_class + '">' + p.toString() + '</span>不匹配.';
}
}
]
])
if (typeof(VanadiumCustomValidationTypes) !== "undefined" && VanadiumCustomValidationTypes) Vanadium.addValidatorTypes(VanadiumCustomValidationTypes);
};
[解决办法]
//ajax判断是否存在值
['ajax',
function (v, p, validation_instance, decoration_context, decoration_callback) {
if (Vanadium.validators_types['empty'].test(v)) return true;
if (decoration_context && decoration_callback) {
jQuery.getJSON(p, {value: v, id: validation_instance.element.id}, function(data) {
decoration_callback.apply(decoration_context, [[data], true]);
});
}
return true;
}]
我是用的MVC来写的 提交到Controller里面啦 但是用户名重复啦 返回什么类型的 data 呢