SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member kantutan's Avatar
    Join Date
    Apr 2008
    Location
    Here
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validation script gone awry

    I am trying to use a javascript to do validation for a textbox "name" & a <select> tag "country":
    Code:
    <script type="text/javascript">
    function validate_required(field,alerttxt)
    {
    with (field)
    {
    if (value==null||value=="")
      {alert(alerttxt);return false;}
    else {return true}
    }
    }function validate_form(thisform)
    {
    var myindex=address.selectedIndex;
    with (thisform)
    {
    if (myindex==0) {
    alert("Please select your country");
    address.focus;return false;
    }
    if (validate_required(name,"Please type in your name")==false)
      {name.focus;return false;}
    }
    }
    </script>
    with this:
    Code:
    <html>
    <head>
    <title>Test</title>
    </head>
    <BODY>
    <form action="insert.php" onsubmit="return validate_form(this)" method="post"> 
    
    <table style="width: 100%;" class="style1" border="0"
     cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td style="width: 176px;">
          <p>Name : </p>
          </td>
          <td>
          <p><input name="name" size="33"
     maxlength="32" id="name" type="text" /></p>
          </td>
        </tr>
    <tr>
          <td>
          <p>Country : </p>
          </td>
          <td>
          <p>
          <select name="country" id="country"
     style="width: 300px;">
          <option value="0" selected="selected">Please
    Choose Your Country</option>
     <option value="1">Australia</option>
          <option value="2">United States</option>
    </select>
          </p>
          </td>
        </tr>
    </tbody>
    </table>
    </form>
    </BODY>
    </HTML>
    Is there a syntax error? 'cuz I got "Duplicate entry '' for key 1".

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You mean that when you send the form without a value for one of the fields, you don't get the error message from the JS, but a MySQL error from your insert.php script? If so, it would seem there's something that isn't working as wanted in your JS.
    If on the other hand you did fill in all form fields, and the PHP script tries to insert a value of '' in the database, it might be an error in the PHP script.

    Anyway, you should always perform the data validation server side, even if you implemented client side JS validation. Because the user might have JS disabled, or the input might arrive from an completely different source (spammers, hackers, whatever) that bypassed your JS validation.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kantutan View Post
    Code:
    var myindex=address.selectedIndex;
    You're trying to validate a non-existent field.
    Also don't call anything 'name', it will overwrite the existing form property.
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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