www.webdeveloper.com
Results 1 to 7 of 7

Thread: Validation data in JS

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    10

    Lightbulb Validation data in JS

    I'm trying to use the function verifica() in JavaScript to validate data populated in the form, but it doesn't run. What's the problem?
    Please, help me in this question.



    Code:
    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO myblog (nome, email, coment) VALUES (%s, %s, %s)",
                           GetSQLValueString($_POST['nome'], "text"),
                           GetSQLValueString($_POST['email'], "text"),
                           GetSQLValueString($_POST['coment'], "text"));
    
      mysql_select_db($database_conn, $conn);
      $Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());
    
      $insertGoTo = "Guest.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    
    mysql_select_db($database_conn, $conn);
    $query_myRdset = "SELECT * FROM myblog ORDER BY id DESC";
    $myRdset = mysql_query($query_myRdset, $conn) or die(mysql_error());
    $row_myRdset = mysql_fetch_assoc($myRdset);
    $totalRows_myRdset = mysql_num_rows($myRdset);
    ?><!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; charset=iso-8859-1" />
    
    body {
    <script language="JavaScript">
        function verifica()
        {
            if(document.form1.nome.value==""){
                window.alert("Por favor, forneça seu nome."); form1.nome.focus();
                return;
                }
                if(document.form1.email.value==""){
                window.alert("Por favor, forneça seu e-mail."); form1.email.focus();
                return;
                }
                if (form1.email.value.indexOf('@',0)==-1||form1.email.value.indexOf('.',0)==-1{window.alert("Por favor, forneça corretamente seu e-mail."); form1.email.focus();
                return;
                }
                if(document.form1.coment.value==""){
                window.alert("Por favor, coloque um comentário."); form1.coment.focus();
                return;
                }
                document.form1.action=<?php echo $editFormAction; ?>;
                document.form1.submit();
                }
    </script>
    
            <table width="100%" border="1" align="center" bgcolor="#CC6666">
      <tr>
        <td><form name="form1" action="#" onsubmit="return verifica()"  method="POST">
        <H3> 
        <div align="center"><em>DEIXE AQUI SEU COMENTÁRIO, DÚVIDA OU SUGESTÃO</em>
          </H2>
            <br />
            <br />
          <span class="style6">Nome:        </span></div>
        <span class="style6">
        <label>
          <input name="nome" type="text" id="nome" size="50" />
          </label>
          </span>
          <p class="style6">E-mail:
            <label>
              <input name="email" type="text" id="email" size="50" maxlength="80" />
              </label>
          </p>
          <p class="style6">Comentário
          <br />
            <label>
            <textarea name="coment" cols="80" rows="5" id="coment"></textarea>
            </label>
          </p>
          <p>
            <input name="submit" type="submit" id="submit" value="Enviar"  />
          </p>
          <p>&nbsp;</p>
          <?php do { ?>
            <p class="style6">Em <?php echo $data = implode("/", array_reverse(explode("-", substr($row_myRdset['data'],0,10)))); ?>  &agrave;s <?php echo substr($row_myRdset['data'],11); ?>
            <?php echo $row_myRdset['nome']; ?>
            (mailto:<?php echo $row_myRdset['email']; ?>
            ) escreveu: <?php echo $row_myRdset['coment']; ?>  </p>
            <?php } while ($row_myRdset = mysql_fetch_assoc($myRdset)); ?><input type="hidden" name="MM_insert" value="form1">
    </form></td>
      </tr>
    </table>
    
        </div>
      </div>
    </div><div style="clear:both;"></div>
    </body>
    </html>
    <?php
    mysql_free_result($myRdset);
    
    mysql_free_result($myRdset);
    ?>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    you should return a false Boolean in order to stop the action.
    Code:
    if(document.form1.nome.value==""){
                window.alert("Por favor, forne&#231;a seu nome."); form1.nome.focus();
                return false;
                }
    // and so on

  3. #3
    Join Date
    Mar 2009
    Location
    Fortaleza, CE, Brazil
    Posts
    81
    Not only that. There were syntax errors in your code.
    The correted code:
    Code:
    function verifica()
    {
            if(document.form1.nome.value==""){
    		window.alert("Por favor, forne&#231;a seu nome."); form1.nome.focus();
    		return false;
    	}
    	if(document.form1.email.value==""){
    		window.alert("Por favor, forne&#231;a seu e-mail."); form1.email.focus();
    		return  false;
    	}
    	if (form1.email.value.indexOf('@',0)==-1||form1.email.value.indexOf('.',0)==-1){
    		window.alert("Por favor, forne&#231;a corretamente seu e-mail."); form1.email.focus();
    		return  false;
    	}
    	if(document.form1.coment.value==""){
    		window.alert("Por favor, coloque um coment&#225;rio."); form1.coment.focus();
    		return  false;
    	}
    	document.form1.action=<?php echo $editFormAction; ?>;
    	document.form1.submit();
    }

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Granite View Post
    Not only that. There were syntax errors in your code.
    The correted code:
    Code:
    function verifica()
    {
            
    	document.form1.action=<?php echo $editFormAction; ?>;
    	document.form1.submit();
    }
    The red part is redundant. Useless. The function is called onsubmit, so that if the function returns anything but false (and by default it will return itself), the submit will be done anyway.

  5. #5
    Join Date
    Mar 2010
    Posts
    10
    Quote Originally Posted by Kor View Post
    The red part is redundant. Useless. The function is called onsubmit, so that if the function returns anything but false (and by default it will return itself), the submit will be done anyway.
    now I got it. I've changed also the last lines putting:

    return true;
    document.form1.submit();

    Now it works.
    Thank you very much.

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by atomicc View Post
    now I got it. I've changed also the last lines putting:

    return true;
    document.form1.submit();

    Now it works.
    Thank you very much.
    As I said. You may remove the line
    Code:
    document.form1.submit();
    as the code will stop anyway on one of the return.

    And you don't need the return true. As I said, if a return is required, the function will return itself (thus not a false Boolean) by default

  7. #7
    Join Date
    Mar 2009
    Location
    Fortaleza, CE, Brazil
    Posts
    81
    Yeah, what he said.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles