我使用 jquery 验证器 1.8 制作了一个简单的验证系统。离线(xampp for mac os 1.7.3)就像一个魅力。当我上网时,验证变得疯狂。选中字段(用户名)的正常行为是,使用 php 文件,用户名在数据库中被实时检查为唯一。在线,直到您进入现场,退出并返回现场,检查才会完成。(这是正常的)然后它开始检查(并且似乎工作)但是当一切顺利时我仍然看到一条错误消息是“1”而不是什么,它被验证器认为是一个错误,停止提交表格......这是代码:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<meta charset="utf-8">
<title>Documento senza titolo</title>
<!-- jquery //-->
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom/js/jquery-ui-1.9.2.custom.js"></script>
<!-- jquery validazioni
<script type="text/javascript" src="js/jquery-validation-1.8.1/jquery.validate.js"></script>
//-->
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript" src="js/jsBetta/messaggiErrore.js"></script>
<!-- datepicker lingua italiana //-->
<script type="text/javascript" src="js/linguaIT/jquery.ui.datepicker-it.js"></script>
<!-- script MIEI (validazione e funzioni) //-->
<script type="text/javascript" src="js/jsBetta/funzioni.js"></script>
<script type="text/javascript" src="js/jsBetta/registrazione.js"></script>
<link rel="stylesheet" href="css/stile.css" />
<link rel="stylesheet" href="js/jquery-ui-1.9.2.custom/css/custom-theme/jquery-ui-1.9.2.custom.css" />
</head>
<body>
<form action="registrazioneDB.php" id="insertUtenteForm" method="post">
<fieldset>
<label for="nome_utente">Username:</label>
<input type="text" name="nome_utente" id="nome_utente" maxlength="30"autocomplete="off">
</fieldset>
<fieldset>
<input id="bottoneSubmit" name="bottoneSubmit" class="submit" type="submit" value="Registrati">
</fieldset>
</form>
</body>
</html>
registrazione.js:
$(document).ready(function(){
var nome_utente;
jQuery('#insertUtenteForm').validate({
rules:
{
nome_utente:
{required: true,
remote: { url:"AdMin/usrAvailCk.php", async:false }}
},
messages: {
nome_utente: { remote: " Username non disponibile"}
},
invalidHandler: function(form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
alert("Si prega di riempire correttamente i campi obbligatori, evidenziati con l'asterisco.");
validator.errorList[0].element.focus();
}
},
});
return false;
});
usrAvailCk.php
<?php
include "connessione.php";
$nome_utente=$_GET['nome_utente'];
$queryUserAvailable = "SELECT * FROM utente where nome_utente='$nome_utente'";
$rsUserAvailable = connetti($queryUserAvailable);
if(mysql_num_rows($rsUserAvailable) == 0){
$valid=true;
}
else{
$valid=false;
}
echo json_encode($valid);
?>
messaggiErrore.js
jQuery.extend(jQuery.validator.messages, {
required: " *",
email: " *",
equalTo: " *",
maxlength: jQuery.validator.format(" *"),
minlength: jQuery.validator.format(" *"),
remote: " *",
});
jQuery(document).ready(function() {
// imposto i dettagli dei messaggi di validazione
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;}, " *");
//$.validator.messages.required = ' *';
//$.validator.messages.email=' *';
});