Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    277

    Form con validazione - errore inserimento

    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!

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    277
    Ciao.

    Potete per favore darmi un aiuto?

    Grazie mille!!

  3. #3
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    A) Che problema riscontri?
    B) Metti la pagina online, si fa prima a vedere il funzionamento e a fare una copia x testarla in locale.
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    277
    Ciao.

    Scusa per il ritardo, ero fuori sede!

    Allora il problema è questo:

    insendo lo username david mi dovrebbe restituire errore, cosa che non fa, sembra non funzionare.

    Se chiamo:

    http://www.ssp-solution.com/test/aja...php?user=david

    risponde 1 o 0 a seconda dello user chiamato, e questo dovrebbe generare l'errore.

    Ho inserito tutto su:



    http://www.ssp-solution.com/test/aja...strazione3.php

    Ti ringrazio moltissimissimo!!!!!!

  5. #5
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    L'operazione è un po + complessa di come l'immaginavo, prova a inserire degli alert sia nel chiamata ajax che nello script di validazione per intercettare i vari step e vedere cosa restituisce.
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    277
    Ti ringrazio.

    Alla fine mi sa che sono dei bachi sullo script js che sto usando, mi sono messo direttamente in contatto con il programmatore per risolvere il problema.

    Grazie mille per tutto,
    Ciao!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.