Ciao a tutti.
Vi chiedo per favore di darmi una mano a sciogliere un problema che ho, che è l'ultimo tassello per completare un minisito che sto facendo.
In pratica sto cercando di usare il codice gvalidator (http://code.google.com/p/gvalidator/) per validare lato client l'inserimento dati in un form.
A questo codice ho inserito un'aggiunta affinché la convalida di un campo non sia solo 'semantica' ma in pratica si vada a verificare l'esistenza o no di un utente, e si restituisca errore nel caso in cui l'utente esiste.
In pratica ci sono 3 script:
1.) registrazione3.php che contiene il form e l'inclusione dei js:
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Registrazione</title>
<script type="text/javascript" src="./common/js/mootools.js"></script>
<script type="text/javascript" src="gvalidator_cus.js"></script>
<script type="text/javascript" src="custom_js.js"></script>
<link rel="stylesheet" type="text/css" href="gvalidator.css" media="screen"/>
</head>
<body>
<form action="/action/" method="post" id="autoform" class="autoform" >
<div><label for="firstname">First Name</label><input type="text" name="firstname" class="name" maxlength="30"/> </div>
<div><label for="lastname">First Name</label><input type="text" name="lastname" class="username" id="lastname" maxlength="30"/></div>
<div><label for="firstname">First Name</label><input type="text" name="subject" class="text" id="subject" maxlength="30"/></div>
<div> <label for="firstname">First Name</label><input type="text" name="captcha" class="captcha" id="captcha" maxlength="30"/></div>
<div> <label for="firstname">First Name</label><input type="text" name="phone" class="phone required" id="phone" maxlength="12"/></div>
<div><input id="submit" type="submit"/></div>
</form>
</body>
</html>
Poi custom_js che è la customizzazione del validatore js:
codice:
ONEGEEK.forms.UsernameTextField = function(field) {
this.field = field;
this.regex = /^([a-zA-Z0-9-\'\s]{8,10})$/g;
this.cleanRegex = /[^0-9a-zA-Z-\'\s]/g
this.errorMsg = 'Your username must be between 8 and 10 characters';
this.contextMsg = 'You will use this to login. It must be between 8 and 10 characters';
this.validate = function() // remove illegal chars
this.clean();
this.errorMessage = 'Please enter a valid email address as your username i.e.
user@domain.com';
// Validate first with regex
var validated = this.field.value.match(this.regex);
// Validate with the server if passes regex
if (validated) {
// Check on the server using SYNCHRONOUS javascript
var myRequest = new Request({method: 'get', url: 'check_user3.php',
async:false});
myRequest.send('user=' + this.field.value);
var response = myRequest.response;
// If there was an ajax error
// allow the form to be submitted and passed again through
// server side validation (as the format is at least correct)
if (response.error) {
validated = true;
}
else {
// If the user exists, return false and vice versa
validated = !response.exists;
this.setModified(true);
this.errorMessage = 'Sorry, account is currently in use.';
}
}
// Set the state
if (validated) {
this.setState(FIELD_STATUS_OK);
return true;
}
else
if (this.modified === false) {
this.setState(FIELD_STATUS_INFO);
}
else {
this.setState(FIELD_STATUS_ERROR);
}
return false;
};
}
// Subclass Abstract FormField class
ONEGEEK.forms.UsernameTextField.prototype = new ONEGEEK.forms.AbstractTextField();
// Register the field types with FormFieldFactory
formFieldFactory.registerFormField('username', 'UsernameTextField');
ed infine il semplice check_user3.php che restitusce uno o zero a seconda che il parametro passato in get sia o no uguale a 'david':
codice:
<?php
$user = $_GET['user'];
if($user == 'david')
{
echo '1'; // disponibile
}else{
echo '0'; // NON disponibile
}
?>
ragazzi, vi sono veramente grato ed una birra a chi riesce a far funzionare questo script!