Jump to content

can't call javascript validation function !!


alvinchua

Recommended Posts

Everything wroks fine but is that when i try to insert null value it only uses the php validation and no alert box appear(i guess it not running the javascript validation)  .. This is the code :

 

 

<html>

<head>

<title>send New MySQL User</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<head>

<script language="JavaScript">

function checkForm()

{

var cfirstname, clastname, cage, cemail;

with(window.document.msgform)

{

cfirstname = firstname;

clastname = lastname;

cage = age;

cemail = email;

 

}

 

if(trim(cfirstname.value) == '')

{

alert('Please enter your name');

cfirstname.focus();

return false;

}

else if(trim(cemail.value) == '')

{

alert('Please enter your email');

cemail.focus();

return false;

}

else if(!isEmail(trim(cemail.value)))

{

alert('Email address is not valid');

cemail.focus();

return false;

}

else if(trim(cage.value) == '')

{

alert('Please enter your message');

cage.focus();

return false;

}

else

{

cname.value = trim(cname.value);

cemail.value = trim(cemail.value);

csubject.value = trim(csubject.value);

cmessage.value = trim(cmessage.value);

return true;

}

}

 

function trim(str)

{

return str.replace(/^\s+|\s+$/g,'');

}

function isEmail(str)

{

var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|

ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|

bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|

ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|

dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|

gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|

hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|

kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|

ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|

mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|no problem|nr|nt|nu|

nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|

re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|

su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|

ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|

zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;

 

return regex.test(str);

}

</script>

</head>

 

<body>

<?php

 

$errmsg = ''; // error message

$firstname = ''; // sender's name

$lastname = ''; // sender's email sendres

$email = ''; // message subject

$age = ''; // the message itself

 

if(isset($_POST['send']))

{

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$email = $_POST['email'];

$age = $_POST['age'];

 

if(trim($firstname) == '')

{

$errmsg = 'Please enter your name';

}

else if(trim($email) == '')

{

$errmsg = 'Please enter your email sendress';

}

else if(!isEmail($email))

{

$errmsg = 'Your email sendress is not valid';

}

else if(trim($lastname) == '')

{

$errmsg = 'Please enter message subject';

}

else if(trim($age) == '')

{

$errmsg = 'Please enter your message';

}

 

if($errmsg == '')

{

if(get_magic_quotes_gpc())

{

$firstname = stripslashes($firstname);

$lastname = stripslashes($lastname);

}

 

if(isset($_POST['send']))

{

$con = mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("login", $con);

 

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$age = $_POST['age'];

$email = $_POST['email'];

 

$query = "INSERT INTO person (firstname,lastname, age, email) VALUES ('$firstname', '$lastname', '$age','$email')";

 

mysql_query($query) or die('Error, insert query failed');

 

$query = "FLUSH PRIVILEGES";

mysql_query($query) or die('Error, insert query failed');

mysql_close($con);

 

echo "New MySQL user addded";

}

}

}

 

if(!isset($_POST['send']) || $errmsg != '')

{

?>

<form method="post" name = "msgform" id = "msgform">

<table width="400" border="0" cellspacing="1" cellpsending="2">

<tr>

<td width="100">Firstname</td>

<td><input name="firstname" type="text" id="firstname"></td>

</tr>

<tr>

<td width="100">Lastname</td>

<td><input name="lastname" type="text" id="lastname"></td>

</tr>

<tr>

<td width="100">Age</td>

<td><input name="age" type="text" id="age" ></td>

</tr>

<tr>

<td width="100">Email</td>

<td><input name="email" type="text" id="email"></td>

</tr>

<td width="100"> </td>

<td> </td>

</tr>

<tr>

<td width="100"> </td>

 

<td><input name="send" type="submit" id="send" value="send Message" onClick = "return checkForm();"></td>

</tr>

</table>

</form>

<?php

}

 

function isEmail($email)

{

return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|no problem|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"

,$email));

}

?>

</body>

</html>

 

???

 

it runs the php validation but not the javascript ?? i did anything wrong in here?? can some1 help me out ?.. this is a code to enter 4 value into the database with the validation the isemail() function is to validation proper email address..

Link to comment
Share on other sites

<form method="post" name = "msgform" id = "msgform" onsubmit="return checkForm();">
<table width="400" border="0" cellspacing="1" cellpsending="2">
<tr>
<td width="100">Firstname</td>
<td><input name="firstname" type="text" id="firstname"></td>
</tr>
<tr>
<td width="100">Lastname</td>
<td><input name="lastname" type="text" id="lastname"></td>
</tr>
<tr>
<td width="100">Age</td>
<td><input name="age" type="text" id="age" ></td>
</tr>
<tr>
<td width="100">Email</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>

<td><input name="send" type="submit" id="send" value="send Message"></td>
</tr>
</table>

 

Should work.

Link to comment
Share on other sites

Change the onclick attribute to an onsubmit attribute and place it as an attribute of the form element.

 

Make sure the onsubmit function returns false if the validation of your form fails.

 

Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.