SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: form validation

  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    SitePoint Member
    Join Date
    May 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, nevermind. got it figured.
    learned about server side validation.

  3. #3
    SitePoint Zealot FaridHadi's Avatar
    Join Date
    Nov 2008
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Sofia,

    It's always a good idea to handle validation on both the server and client side. Server side for security reasons, as anyone could disable JavaScript and get passed your client side validation, and client side for improved usability.

    There are a few issues with the code you've posted. E.g. "IsEmpty" and "IsNumeric" aren't JavaScript functions. I've re-written your function just to show you how you could go about doing it.

    Code JavaScript:
    <script type="text/javascript">
    function ValidateForm(form) {
    	if(form.Nationality.value=='') {
    		alert('You have not entered an account number');
    		form.Nationality.focus();
    		return false;
    	}
     
    	if(isNaN(form.Nationality.value)) {
    		alert('Please enter only numbers or decimal points in the account field');
    		form.Nationality.focus();
    		return false;
    	}
     
    	return true;
    }
    </script>

    Try that out and you'll see that it works.

    Let us know if you need any more help.

  4. #4
    SitePoint Member
    Join Date
    May 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help FaridHadi, i think i got it to work now. ahh so much to learn.

    much appreciated all the help.

  5. #5
    SitePoint Zealot FaridHadi's Avatar
    Join Date
    Nov 2008
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome Sofia.

    Yes, if you're just starting out there's a lot to learn In fact, there's always a lot to learn in this ever evolving world of web development.

    Make sure to make Google, or whichever search engine you prefer, your best friend and keep coming back here whenever you get stuck.


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
  •