www.webdeveloper.com
Results 1 to 3 of 3

Thread: form validation

  1. #1
    Join Date
    Apr 2011
    Posts
    4

    form validation

    Hello,

    Im quite new to javascript and php, so dont flame me too hard

    Could someone take a look at my code and tell me what am i doing wrong for form validation? I was able to get it to work with on click function but failing with the example below. I have a form with options to pick from and php that submits the values to mysql. All i need is for some of the forms to be validated before the values are entered into database, right now the form is entered into db and no warning saying that fields are empty comes up. Any help is much appreciated.


    thanks!
    Sofia


    <HEAD>
    <script type="text/javascript">
    function ValidateForm(form)
    {

    if (IsEmpty(form.Nationality))
    {
    alert('You have not entered an account number')
    form.Nationality.focus();
    return false;
    }


    if (!IsNumeric(form.Nationality.value))
    {
    alert('Please enter only numbers or decimal points in the account field')
    form.Nationality.focus();
    return false;
    }

    return true;

    }

    </script>
    </HEAD>

    <BODY>
    <form name="form" method="post" onsubmit="return ValidateForm(this)">
    <div><select name="Nationality">
    <option value=""></option>
    <option value="Afghanistan">Afghanistan</option>
    <option value="Albania">Albania</option>
    <option value="Algeria">Algeria</option>
    </select>


    <?php
    if (isset($_REQUEST['submit'])) {
    $sql = "INSERT INTO $db_table(Nationality, Country) values ('".mysql_real_escape_string(stripslashes($_REQUEST['Nationality']))."',,'".mysql_real_escape_string(stripslashes($_REQUEST['Country']))."')";
    if($result = mysql_query($sql ,$db)) {
    echo '<h1>Success!</h1><br><br>';
    } else {
    echo "ERROR: ".mysql_error();
    }
    } else {
    ?>


    <input type="submit" name="submit" value="Submit">
    </form>
    </BODY>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You must have copied a validation code from elsewhere. First: you call two functions IsEmpty() and isNumeric() which do not exist. They must have been declared in the code where you have copied this part of the code. But you missed to copy/paste them as well.

    On the other hand, it looks like you did not understand what those function suppose to do:
    1. IsEmpty() must have been checking whether a text box or a textarea is empty.
    2. IsNumeric must have been checking whether a text box or a textarea has numeric values.

    But this is not your case. You have a select box. To check if a select box is selected, you may check whether its selectedIndex property is>0

    http://www.w3schools.com/jsref/prop_...ectedindex.asp

  3. #3
    Join Date
    Apr 2011
    Posts
    4
    Thanks for your help Kor

    learned something new

Thread Information

Users Browsing this Thread

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

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