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>

ok, nevermind. got it figured.
learned about server side validation. :slight_smile:

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.


<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.

Thanks for your help FaridHadi, i think i got it to work now. ahh so much to learn.

much appreciated all the help.

You’re welcome Sofia.

Yes, if you’re just starting out there’s a lot to learn :slight_smile: 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.