By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

URGENT: form validation

100+
P: 274
I am trying form valdiation with javascript but it never call the function. can someone please test this code and see why it is like that. It screwed up my mind.
here is my code:
Expand|Select|Wrap|Line Numbers
  1. <!-- Java Script form form validation and entries -->
  2. <script language="JavaScript">
  3. function checkForm()
  4. {
  5. alert("I am called");
  6.    var cfirstname, cemail, cpass, clastname;
  7.  
  8.    with(window.document.msgform)
  9.    {
  10.       cfirstname    = firstname;
  11.       cemail   = email;
  12.       cpass = pass;
  13.       clastname = lastname;
  14.    }
  15.  
  16.    if(trim(cfirstname.value) == '')
  17.    {
  18.       alert('Please enter your first name');
  19.       cfirstname.focus();
  20.       return false;
  21.    }
  22.    else if(trim(cemail.value) == '')
  23.    {
  24.       alert('Please enter your email');
  25.       cemail.focus();
  26.       return false;
  27.    }
  28.    else if(!isEmail(trim(cemail.value))) // validate email address
  29.    {
  30.       alert('Email address is not valid');
  31.       cemail.focus();
  32.       return false;
  33.    }
  34.    else if(trim(cpass.value) == '')
  35.    {
  36.       alert('Please enter your password');
  37.       cpass.focus();
  38.       return false;
  39.    }
  40.    else if(trim(clastname.value) == '')
  41.    {
  42.       alert('Please enter your lastname');
  43.       clastname.focus();
  44.       return false;
  45.    }
  46.    else
  47.    {
  48.       cfirstname.value    = trim(cfirstname.value);
  49.       cemail.value   = trim(cemail.value);
  50.       cpass.value = trim(cpass.value);
  51.       clastname.value = trim(clastname.value);
  52.       return true;
  53.    }
  54. }
  55.  
  56. function trim(str)
  57. {
  58.    return str.replace(/^\s+|\s+$/g,'');
  59. }
  60.  
  61. function isEmail(str)
  62. {
  63.    var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|
  64. ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|
  65. bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|
  66. ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|
  67. dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|
  68. gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|
  69. hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|
  70. kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|
  71. ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|
  72. mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|
  73. nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|
  74. re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|
  75. su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|
  76. ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|
  77. 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;
  78.  
  79. return regex.test(str);
  80. }
  81. </script>
  82.  
  83.  
  84. <?php 
  85. // Connects to your Database 
  86. include_once ('connection.php');
  87. include_once ('class.countries.inc.php');
  88. //This code runs if the form has been submitted
  89. if (isset($_POST['submit'])) { 
  90. echo("going to check");
  91. echo "<SCRIPT LANGUAGE='javascript'>checkForm();</SCRIPT>";
  92. //This makes sure they did not leave any fields blank
  93. if (!$_POST['firstname'] |!$_POST['lastname'] |!$_POST['email'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['company'] | !$_POST['address1']| !$_POST['city']| !$_POST['province']
  94.     | !$_POST['country']| !$_POST['postalcode']| !$_POST['phone']| !$_POST['sub1']| !$_POST['sub2']
  95.     ) {
  96. die('You did not complete all of the required fields');
  97. }
  98. // checks if the username is in use
  99. if (!get_magic_quotes_gpc()) {
  100.     $_POST['email'] = addslashes($_POST['email']);
  101. }
  102.     $usercheck = $_POST['email'];
  103.     $check = mysql_query("SELECT email FROM member WHERE email = '$usercheck'") 
  104.     or die(mysql_error());
  105.     $check2 = mysql_num_rows($check);
  106. //check if the name exists it gives an error
  107. if ($check2 != 0) {
  108.     die('Sorry, the email '.$_POST['email'].' is already in use.');
  109. }
  110. //  makes sure both passwords entered match
  111. if ($_POST['pass'] != $_POST['pass2']) {
  112.     die('Your passwords did not match. ');
  113. }
  114. // encrypt the password and add slashes if needed
  115.     $_POST['pass'] = md5($_POST['pass']);
  116. // check for addslashes    
  117. if (!get_magic_quotes_gpc()) {
  118.     $_POST['firstname'] = addslashes($_POST['firstname']);
  119.     $_POST['lastname'] = addslashes($_POST['lastname']);
  120.     $_POST['pass'] = addslashes($_POST['pass']);
  121.     $_POST['email'] = addslashes($_POST['email']);
  122.     $_POST['company'] = addslashes($_POST['company']);
  123.     $_POST['address1'] = addslashes($_POST['address1']);
  124.     $_POST['city'] = addslashes($_POST['city']);
  125.     $_POST['province'] = addslashes($_POST['province']);
  126.     $_POST['country'] = addslashes($_POST['country']);
  127.     $_POST['postalcode'] = addslashes($_POST['postalcode']);
  128.     $_POST['phone'] = addslashes($_POST['phone']);
  129.     $_POST['sub1'] = addslashes($_POST['sub1']);
  130.     $_POST['sub2'] = addslashes($_POST['sub2']);
  131. }
  132.  
  133. //  insert it into the database
  134. /*     $insert = "INSERT INTO member (firstname,lastname,company,region,title,address1,address2,city,province,country,postalcode,
  135.                 phone,fax,email,password,website,noofsubscriber,noofaddressablesubscriber)
  136.         VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['company']."', '".$_POST['region']."',
  137.                 '".$_POST['title']."', '".$_POST['address1']."', '".$_POST['address2']."', '".$_POST['city']."', '".$_POST['province']."',
  138.                 '".$_POST['country']."',' ".$_POST['postalcode']."', '".$_POST['phone']."', '".$_POST['fax']."', '".$_POST['email']."', '".$_POST['pass']."',
  139.                 '".$_POST['website']."', '".$_POST['sub1']."', '".$_POST['sub2']."')";
  140.         $add_member = mysql_query($insert); */
  141.         $insert = "INSERT INTO member (firstname,lastname,company,region,title,
  142.                    address1,address2,city,province,country,postalcode,
  143.                    phone,ext,fax,email,password,website,noofsubscriber,noofaddressablesubscriber)
  144.         VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['company']."', '".$_POST['region']."', '".$_POST['title']."',
  145.                 '".$_POST['address1']."', '".$_POST['address2']."', '".$_POST['city']."', '".$_POST['province']."', '".$_POST['country']."', '".$_POST['postalcode']."',
  146.                 '".$_POST['phone']."', '".$_POST['ext']."', '".$_POST['fax']."', '".$_POST['email']."', '".$_POST['pass']."',
  147.                 '".$_POST['website']."', '".$_POST['sub1']."', '".$_POST['sub2']."')";
  148.         $add_member = mysql_query($insert);
  149. ?>
  150. <h1>Registered</h1>
  151. <p>Thank you, you have registered - you may now login</a>.</p>
  152. <?php 
  153. else 
  154. ?>
  155. <!-- action="<php echo $_SERVER['PHP_SELF']; ?>" -->
  156. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="msgform" >
  157. <table border="0">
  158. <tr><td>First Name:</td><td>
  159. <input type="text" name="firstname" maxlength="50" size=50>
  160. </td></tr>
  161. <tr><td>Last Name:</td><td>
  162. <input type="text" name="lastname" maxlength="50" size=50>
  163. </td></tr>
  164. <tr><td>Company:</td><td>
  165. <input type="text" name="company" maxlength="150" size=60>
  166. </td></tr>
  167. <tr><td>Divion/Region:</td><td>
  168. <input type="text" name="region" maxlength="50" size=50>
  169. </td></tr>
  170. <tr><td>Title:</td><td>
  171. <input type="text" name="title" maxlength="50" size=50>
  172. </td></tr>
  173. <tr><td>Address 1:</td><td>
  174. <input type="text" name="address1" maxlength="150" size=60>
  175. </td></tr>
  176. <tr><td>Address 2:</td><td>
  177. <input type="text" name="address2" maxlength="150" size=60>
  178. </td></tr>
  179. <tr><td>City:</td><td>
  180. <input type="text" name="city" maxlength="50" size=50>
  181. </td></tr>
  182. <tr><td>State/Province:</td><td>
  183. <input type="text" name="province" maxlength="50" size=50>
  184. </td></tr>
  185. <tr><td>Country:</td><td>
  186. <!-- <input type="text" name="country" maxlength="50"> -->
  187. <?php
  188. $countryObj = new countries;
  189.       $countryselect = $countryObj->countrySelect(
  190.          array('name'=>'country',
  191.               'style'=>'width: 200px; color: blue;'
  192.       ),$country?$country:"ca");
  193.  
  194.    echo $countryselect;
  195.   ?>
  196. </td></tr>
  197. <tr><td>Zip/Postal Code:</td><td>
  198. <input type="text" name="postalcode" maxlength="6" size=50>
  199. </td></tr>
  200. <tr><td>Phone:</td><td>
  201. <input type="text" name="phone" maxlength="15"> Ext: <input type="text" name="ext" maxlength="4" size=4>
  202. </td></tr>
  203. <tr><td>Fax:</td><td>
  204. <input type="text" name="fax" maxlength="15" size=50>
  205. </td></tr>
  206. <tr><td>Email:</td><td>
  207. <input type="text" name="email" maxlength="100" size=60>
  208. </td></tr>
  209. <tr><td>Password:</td><td>
  210. <input type="password" name="pass" maxlength="10" size=12>
  211. </td></tr>
  212. <tr><td>Confirm Password:</td><td>
  213. <input type="password" name="pass2" maxlength="10" size=12>
  214. </td></tr>
  215. <tr><td>Website:</td><td>
  216. <input type="text" name="website" maxlength="100" size=60>
  217. </td></tr>
  218. <tr><td># of Subscribes:</td><td>
  219. <input type="text" name="sub1" maxlength="5" size=4>
  220. </td></tr>
  221. <tr><td># of Addressable Subscribers:</td><td>
  222. <input type="text" name="sub2" maxlength="5" size=4>
  223. </td></tr>
  224. <tr><th colspan=2><input type="submit" name="submit" value="Submit Registeration" ></th></tr> </table>
  225. </form>
  226.  
  227. <?php
  228. }
  229. ?> 
  230.  
  231.  
  232.  
Jul 31 '08 #1
Share this Question
Share on Google+
7 Replies


Atli
Expert 5K+
P: 5,058
Are you calling the JavaScript function to check the fields after they have been submitted? That won't work. At that point there will be no fields to check.

Try attaching the JavaScript check function to the forms onsubmit event, or the submit buttons onclick event.

Also, the huge regex variable in your JavaScript code contains line-breaks, which is not allowed.
Jul 31 '08 #2

Markus
Expert 5K+
P: 6,050
If the *Javascript* function isn't called, then this *isn't* a PHP problem.
Jul 31 '08 #3

pbmods
Expert 5K+
P: 5,821
If the *Javascript* function isn't called, then this *isn't* a PHP problem.
Agreed. Moving to the JavaScript forum....

Now then.

Try adding an onsubmit handler to your form.
Jul 31 '08 #4

100+
P: 274
I am lost in this combination. I tried everything thing onsubmit() for form, action for form and onlick for submit button. Nothing worked at all except time waste. I alwasy prefer javascript for form validation but now...I dropped the idea to use javascript for php form validation, and used php-form-validation functions for this purpose. It took me a while to write that but that worked . If some want to post how i did that I'll be happy to do that.
Thanks All
Aug 1 '08 #5

gits
Expert Mod 5K+
P: 5,344
please show how you tried to call the 'checkForm'-method on submit ... the validation must return a boolean value and the line should be similar to the following:

Expand|Select|Wrap|Line Numbers
  1. <form name="my_form" onsubmit="return checkForm();"/>
kind regards
Aug 1 '08 #6

RamananKalirajan
100+
P: 607
Hi Dude, this is a simple javascript validation. just try this code this may help you

[HTML]</html>
<head>
<script type="text/javascript">
function validate()
{
var chars = document.getElementById('field').value;
if (chars.length < 130)
{
alert("The news story content must be a minimum of 130 characters to properly display. Please enter more content and re-submit the form.");
return false;
}
else
{
return true;
}
}
</script>
</head>
<form action="demo.html" method="post" onSubmit="return validate()">
<textarea id="field">
</textarea>
<input type="submit" value="Submit Me">
</form>
</html>[/HTML]

Regards
Ramanan Kalirajan
Aug 1 '08 #7

acoder
Expert Mod 15k+
P: 16,027
I am lost in this combination. I tried everything thing onsubmit() for form, action for form and onlick for submit button. Nothing worked at all except time waste. I alwasy prefer javascript for form validation but now...I dropped the idea to use javascript for php form validation, and used php-form-validation functions for this purpose.
That was a big mistake in the first place. You should never depend on JavaScript validation alone. It's only there as a convenience to avoid unnecessary trips to the server. You MUST have server-side validation at all times.
Aug 1 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.