实体类
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
uphone = db.Column(db.String(11))
html/demo.html
<input type="text" id="uphone" onblur="checkPhone()">
<span id="uphone-show"></span>
javascript/demo.html
<script src="/static/js/common.js"></script>
<script src="/static/js/jquery-1.11.3.js"></script>
<script>
function checkPhone() {
var xhr = createXhr();
xhr.open('get', '/check?uphone='+$("#uphone").val(), true);
xhr.onreadystatechange = function(){
if(xhr.readyState==4 && xhr.status==200){
$("#uphone-show").html(xhr.responseText);
}
};
xhr.send(null);
}
</script>
javascript/common.js
function createXhr(){
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}else{
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
视图
@main.route('/')
def index():
return render_template('demo.html')
@main.route('/check')
def check():
uphone = request.args['uphone']
user = User.query.filter_by(uphone=uphone).first()
if user:
return "手机号码已存在"
else:
return "通过"