Solved

Javascript form validation

Posted on 2013-12-22
6
873 Views
Last Modified: 2013-12-24
I have created the below html form to create a new user, and I have added a lot of validation checks using the javascript, the problem is once I press submit I can notice momentarily the wrong fields are turned into yellow, but the form is posted anyway, how can stop posting the form in case the validation fails?

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Create User</title>
	<link rel="stylesheet" href="js/themes/base/jquery.ui.all.css">
	<script src="js/jquery-1.9.1.js"></script>
	<script src="js/ui/jquery.ui.core.js"></script>
	<script src="js/ui/jquery.ui.widget.js"></script>
	<script src="js/ui/jquery.ui.tabs.js"></script>
	<script src="js/ui/jquery.ui.effect.js"></script>
	<script src="js/ui/jquery.ui.effect-drop.js"></script>
	<script src="js/ui/jquery.ui.datepicker.js"></script>
        <script type="text/javascript" src="js/passtest.js"></script>
	<link rel="stylesheet" href="css/demos.css">
	<script>
	$(function() {
		$( "#tabs" ).tabs();
                $( "#birthdate" ).datepicker({changeMonth: true,changeYear: true});
		$( "#birthdate" ).datepicker( "option", "showAnim", "drop" );
                $( "#birthdate" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
                $( "input[type=submit], a, button" ).button().click(function( event ) {event.preventDefault();});
	});
                function disabledropbox() {
	          document.createuser.dropboxquota.value='';
	          document.createuser.dropboxquota.disabled=true;
                }
                function enabledropbox() {
	          document.createuser.dropboxquota.disabled=false;
                }
                function disablewebconf() {
                  document.createuser.webconfquota.value='';
                  document.createuser.webconfquota.disabled=true;
                }
                function enablewebconf() {
                  document.createuser.webconfquota.disabled=false;
                }
                function disablewebpublish() {
                  document.createuser.webpublishquota.value='';
                  document.createuser.webpublishquota.disabled=true;
                }
                function enablewebpublish() {
                  document.createuser.webpublishquota.disabled=false;
                }
                function disableinternalphone() {
                  document.createuser.intphonenum.value='';
                  document.createuser.intphonenum.disabled=true;
                  document.getElementById('calloutsideN').checked=true;
                  document.getElementById('recieveoutsideN').checked=true;
                  document.getElementById('teleconfN').checked=true;
                }
                function enableinternalphone() {
                  document.createuser.intphonenum.disabled=false;
                  document.getElementById('calloutsideY').checked=true;
                  document.getElementById('recieveoutsideY').checked=true;
                  document.getElementById('teleconfY').checked=true;
                }
		function validateFormOnSubmit(theForm) {
			var reason = "";

	  		reason += validateUsername(theForm.username);
  			reason += validatePassword(theForm.password);
  			reason += validateEmail(theForm.extemail);
 		 	reason += validatePhone(theForm.mobile);
 		 	reason += validatePhone(theForm.phone);
 		 	reason += validatePhone(theForm.fax);
  			reason += validateEmpty(theForm.from);

  			if (reason != "") {
    				alert("Some fields need correction:\n" + reason);
    				return false;
  			}
                        return true;
		}
		function validateEmpty(fld) {
    			var error = "";

	    		if (fld.value.length == 0) {
        			fld.style.background = 'Yellow';
        			error = "The required field has not been filled in.\n"
    			} else {
        			fld.style.background = 'White';
    			}
    			return error;
		}
		function validateUsername(fld) {
    			var error = "";
    			var illegalChars = /\W/; // allow letters, numbers, and underscores

    			if (fld.value == "") {
        			fld.style.background = 'Yellow';
        			error = "You didn't enter a username.\n";
    			} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        			fld.style.background = 'Yellow';
        			error = "The username is the wrong length.\n";
    			} else if (illegalChars.test(fld.value)) {
        			fld.style.background = 'Yellow';
        			error = "The username contains illegal characters.\n";
    			} else {
        			fld.style.background = 'White';
    			}
    			return error;
		}
		function validatePassword(fld) {
    			var error = "";
    			var illegalChars = /[\W_]/; // allow only letters and numbers

    			if (fld.value == "") {
        			fld.style.background = 'Yellow';
        			error = "You didn't enter a password.\n";
    			} else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        			error = "The password is the wrong length. \n";
        			fld.style.background = 'Yellow';
    			} else if (illegalChars.test(fld.value)) {
        			error = "The password contains illegal characters.\n";
        			fld.style.background = 'Yellow';
    			} else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        			error = "The password must contain at least one numeral.\n";
        			fld.style.background = 'Yellow';
    			} else {
        			fld.style.background = 'White';
    			}
   			return error;
		}
		function trim(s)
		{
  			return s.replace(/^\s+|\s+$/, '');
		}

		function validateEmail(fld) {
    			var error="";
    			var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    			var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    			if (fld.value == "") {
        			fld.style.background = 'Yellow';
        			error = "You didn't enter an email address.\n";
    			} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        			fld.style.background = 'Yellow';
        			error = "Please enter a valid email address.\n";
    			} else if (fld.value.match(illegalChars)) {
        			fld.style.background = 'Yellow';
        			error = "The email address contains illegal characters.\n";
    			} else {
        			fld.style.background = 'White';
    			}
    			return error;
		}
		function validatePhone(fld) {
    			var error = "";
    			var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   			if (fld.value == "") {
        			error = "You didn't enter a phone number.\n";
        			fld.style.background = 'Yellow';
    				} else if (isNaN(parseInt(stripped))) {
        				error = "The phone number contains illegal characters.\n";
        				fld.style.background = 'Yellow';
    				} else if (!(stripped.length == 10)) {
        				error = "The phone number is the wrong length. Make sure you included an area code.\n";
        				fld.style.background = 'Yellow';
    			}
    			return error;
		}

	</script>
</head>
<body>
<form action="storesubscriber.php" name="createuser" method="POST" onsubmit="return validateFormOnSubmit(this)">
   <div  style="font-family: Helvetica, sans-serif;color: #444;-webkit-font-smoothing: antialiased;">
<div id="tabs">
	<ul>
		<li><a href="#tabs-1">Personal Details</a></li>
		<li><a href="#tabs-2">Contact Details</a></li>
		<li><a href="#tabs-3">Professional Details</a></li>
	</ul>
	<div id="tabs-1" `>
		<table>
                  <tr>
                  <td>
                      Title
                  </td>
                  <td>
                     <select name="title">
                        <option value="Mr">Mr</option>
                        <option value="Miss">Miss</option>
                        <option value="Mrs">Mrs</option>
                        <option value="Prof">Prof</option>
                        <option value="Dr">Dr</option>
                        <option value="Eng">Eng</option>
                     </select> 
                  </td>
                  </tr>
                  <tr>
                  <td>
                     First Name 
                  </td>
                  <td>
                     <input type="text" name="firstname" maxlength="15" size="15" />
                  </td>
                  <td>
                     Last Name
                  </td>
                  <td>
                     <input type="text" name="lastname" maxlength="15" size="15" />
                  </td>
                  </tr>
                  <tr>
                  <td>
                     Address
                  </td>
                  <td>
                     <input type="text" name="address" maxlength="50" size="50"  />
                  </td>
                  </tr>
                  <tr>
                  <td>
                    City 
                  </td>
                  <td>
                     <input type="text" name="city" maxlength="10" size="10" />
                  </td>
                  <td>
                      Country
                  </td>
                  <td>
                     <select name="country">
			<option value="" selected="selected">Select Country</option>
			<option value="United States">United States</option>
			<option value="United Kingdom">United Kingdom</option>
			<option value="Afghanistan">Afghanistan</option>
			<option value="Albania">Albania</option>
			<option value="Algeria">Algeria</option>
			<option value="American Samoa">American Samoa</option>
			<option value="Andorra">Andorra</option>
			<option value="Angola">Angola</option>
			<option value="Anguilla">Anguilla</option>
			<option value="Antarctica">Antarctica</option>
			<option value="Antigua and Barbuda">Antigua and Barbuda</option>
			<option value="Argentina">Argentina</option>
			<option value="Armenia">Armenia</option>
			<option value="Aruba">Aruba</option>
			<option value="Australia">Australia</option>
			<option value="Austria">Austria</option>
			<option value="Azerbaijan">Azerbaijan</option>
			<option value="Bahamas">Bahamas</option>
			<option value="Bahrain">Bahrain</option>
			<option value="Bangladesh">Bangladesh</option>
			<option value="Barbados">Barbados</option>
			<option value="Belarus">Belarus</option>
			<option value="Belgium">Belgium</option>
			<option value="Belize">Belize</option>
			<option value="Benin">Benin</option>
			<option value="Bermuda">Bermuda</option>
			<option value="Bhutan">Bhutan</option>
			<option value="Bolivia">Bolivia</option>
			<option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option>
			<option value="Botswana">Botswana</option>
			<option value="Bouvet Island">Bouvet Island</option>
			<option value="Brazil">Brazil</option>
			<option value="British Indian Ocean Territory">British Indian Ocean Territory</option>
			<option value="Brunei Darussalam">Brunei Darussalam</option>
			<option value="Bulgaria">Bulgaria</option>
			<option value="Burkina Faso">Burkina Faso</option>
			<option value="Burundi">Burundi</option>
			<option value="Cambodia">Cambodia</option>
			<option value="Cameroon">Cameroon</option>
			<option value="Canada">Canada</option>
			<option value="Cape Verde">Cape Verde</option>
			<option value="Cayman Islands">Cayman Islands</option>
			<option value="Central African Republic">Central African Republic</option>
			<option value="Chad">Chad</option>
			<option value="Chile">Chile</option>
			<option value="China">China</option>
			<option value="Christmas Island">Christmas Island</option>
			<option value="Cocos (Keeling) Islands">Cocos (Keeling) Islands</option>
			<option value="Colombia">Colombia</option>
			<option value="Comoros">Comoros</option>
			<option value="Congo">Congo</option>
			<option value="Congo, The Democratic Republic of The">Congo, The Democratic Republic of The</option>
			<option value="Cook Islands">Cook Islands</option>
			<option value="Costa Rica">Costa Rica</option>
			<option value="Cote D'ivoire">Cote D'ivoire</option>
			<option value="Croatia">Croatia</option>
			<option value="Cuba">Cuba</option>
			<option value="Cyprus">Cyprus</option>
			<option value="Czech Republic">Czech Republic</option>
			<option value="Denmark">Denmark</option>
			<option value="Djibouti">Djibouti</option>
			<option value="Dominica">Dominica</option>
			<option value="Dominican Republic">Dominican Republic</option>
			<option value="Ecuador">Ecuador</option>
			<option value="Egypt">Egypt</option>
			<option value="El Salvador">El Salvador</option>
			<option value="Equatorial Guinea">Equatorial Guinea</option>
			<option value="Eritrea">Eritrea</option>
			<option value="Estonia">Estonia</option>
			<option value="Ethiopia">Ethiopia</option>
			<option value="Falkland Islands (Malvinas)">Falkland Islands (Malvinas)</option>
			<option value="Faroe Islands">Faroe Islands</option>
			<option value="Fiji">Fiji</option>
			<option value="Finland">Finland</option>
			<option value="France">France</option>
			<option value="French Guiana">French Guiana</option>
			<option value="French Polynesia">French Polynesia</option>
			<option value="French Southern Territories">French Southern Territories</option>
			<option value="Gabon">Gabon</option>
			<option value="Gambia">Gambia</option>
			<option value="Georgia">Georgia</option>
			<option value="Germany">Germany</option>
			<option value="Ghana">Ghana</option>
			<option value="Gibraltar">Gibraltar</option>
			<option value="Greece">Greece</option>
			<option value="Greenland">Greenland</option>
			<option value="Grenada">Grenada</option>
			<option value="Guadeloupe">Guadeloupe</option>
			<option value="Guam">Guam</option>
			<option value="Guatemala">Guatemala</option>
			<option value="Guinea">Guinea</option>
			<option value="Guinea-bissau">Guinea-bissau</option>
			<option value="Guyana">Guyana</option>
			<option value="Haiti">Haiti</option>
			<option value="Heard Island and Mcdonald Islands">Heard Island and Mcdonald Islands</option>
			<option value="Holy See (Vatican City State)">Holy See (Vatican City State)</option>
			<option value="Honduras">Honduras</option>
			<option value="Hong Kong">Hong Kong</option>
			<option value="Hungary">Hungary</option>
			<option value="Iceland">Iceland</option>
			<option value="India">India</option>
			<option value="Indonesia">Indonesia</option>
			<option value="Iran, Islamic Republic of">Iran, Islamic Republic of</option>
			<option value="Iraq">Iraq</option>
			<option value="Ireland">Ireland</option>
			<option value="Israel">Israel</option>
			<option value="Italy">Italy</option>
			<option value="Jamaica">Jamaica</option>
			<option value="Japan">Japan</option>
			<option value="Jordan">Jordan</option>
			<option value="Kazakhstan">Kazakhstan</option>
			<option value="Kenya">Kenya</option>
			<option value="Kiribati">Kiribati</option>
			<option value="Korea, Democratic People's Republic of">Korea, Democratic People's Republic of</option>
			<option value="Korea, Republic of">Korea, Republic of</option>
			<option value="Kuwait">Kuwait</option>
			<option value="Kyrgyzstan">Kyrgyzstan</option>
			<option value="Lao People's Democratic Republic">Lao People's Democratic Republic</option>
			<option value="Latvia">Latvia</option>
			<option value="Lebanon">Lebanon</option>
			<option value="Lesotho">Lesotho</option>
			<option value="Liberia">Liberia</option>
			<option value="Libyan Arab Jamahiriya">Libyan Arab Jamahiriya</option>
			<option value="Liechtenstein">Liechtenstein</option>
			<option value="Lithuania">Lithuania</option>
			<option value="Luxembourg">Luxembourg</option>
			<option value="Macao">Macao</option>
			<option value="Macedonia, The Former Yugoslav Republic of">Macedonia, The Former Yugoslav Republic of</option>
			<option value="Madagascar">Madagascar</option>
			<option value="Malawi">Malawi</option>
			<option value="Malaysia">Malaysia</option>
			<option value="Maldives">Maldives</option>
			<option value="Mali">Mali</option>
			<option value="Malta">Malta</option>
			<option value="Marshall Islands">Marshall Islands</option>
			<option value="Martinique">Martinique</option>
			<option value="Mauritania">Mauritania</option>
			<option value="Mauritius">Mauritius</option>
			<option value="Mayotte">Mayotte</option>
			<option value="Mexico">Mexico</option>
			<option value="Micronesia, Federated States of">Micronesia, Federated States of</option>
			<option value="Moldova, Republic of">Moldova, Republic of</option>
			<option value="Monaco">Monaco</option>
			<option value="Mongolia">Mongolia</option>
			<option value="Montserrat">Montserrat</option>
			<option value="Morocco">Morocco</option>
			<option value="Mozambique">Mozambique</option>
			<option value="Myanmar">Myanmar</option>
			<option value="Namibia">Namibia</option>
			<option value="Nauru">Nauru</option>
			<option value="Nepal">Nepal</option>
			<option value="Netherlands">Netherlands</option>
			<option value="Netherlands Antilles">Netherlands Antilles</option>
			<option value="New Caledonia">New Caledonia</option>
			<option value="New Zealand">New Zealand</option>
			<option value="Nicaragua">Nicaragua</option>
			<option value="Niger">Niger</option>
			<option value="Nigeria">Nigeria</option>
			<option value="Niue">Niue</option>
			<option value="Norfolk Island">Norfolk Island</option>
			<option value="Northern Mariana Islands">Northern Mariana Islands</option>
			<option value="Norway">Norway</option>
			<option value="Oman">Oman</option>
			<option value="Pakistan">Pakistan</option>
			<option value="Palau">Palau</option>
			<option value="Palestinian Territory, Occupied">Palestinian Territory, Occupied</option>
			<option value="Panama">Panama</option>
			<option value="Papua New Guinea">Papua New Guinea</option>
			<option value="Paraguay">Paraguay</option>
			<option value="Peru">Peru</option>
			<option value="Philippines">Philippines</option>
			<option value="Pitcairn">Pitcairn</option>
			<option value="Poland">Poland</option>
			<option value="Portugal">Portugal</option>
			<option value="Puerto Rico">Puerto Rico</option>
			<option value="Qatar">Qatar</option>
			<option value="Reunion">Reunion</option>
			<option value="Romania">Romania</option>
			<option value="Russian Federation">Russian Federation</option>
			<option value="Rwanda">Rwanda</option>
			<option value="Saint Helena">Saint Helena</option>
			<option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option>
			<option value="Saint Lucia">Saint Lucia</option>
			<option value="Saint Pierre and Miquelon">Saint Pierre and Miquelon</option>
			<option value="Saint Vincent and The Grenadines">Saint Vincent and The Grenadines</option>
			<option value="Samoa">Samoa</option>
			<option value="San Marino">San Marino</option>
			<option value="Sao Tome and Principe">Sao Tome and Principe</option>
			<option value="Saudi Arabia">Saudi Arabia</option>
			<option value="Senegal">Senegal</option>
			<option value="Serbia and Montenegro">Serbia and Montenegro</option>
			<option value="Seychelles">Seychelles</option>
			<option value="Sierra Leone">Sierra Leone</option>
			<option value="Singapore">Singapore</option>
			<option value="Slovakia">Slovakia</option>
			<option value="Slovenia">Slovenia</option>
			<option value="Solomon Islands">Solomon Islands</option>
			<option value="Somalia">Somalia</option>
			<option value="South Africa">South Africa</option>
			<option value="South Georgia and The South Sandwich Islands">South Georgia and The South Sandwich Islands</option>
			<option value="Stateless">Stateless</option>
			<option value="Spain">Spain</option>
			<option value="Sri Lanka">Sri Lanka</option>
			<option value="Sudan">Sudan</option>
			<option value="Suriname">Suriname</option>
			<option value="Svalbard and Jan Mayen">Svalbard and Jan Mayen</option>
			<option value="Swaziland">Swaziland</option>
			<option value="Sweden">Sweden</option>
			<option value="Switzerland">Switzerland</option>
			<option value="Syrian Arab Republic">Syrian Arab Republic</option>
			<option value="Taiwan, Province of China">Taiwan, Province of China</option>
			<option value="Tajikistan">Tajikistan</option>
			<option value="Tanzania, United Republic of">Tanzania, United Republic of</option>
			<option value="Thailand">Thailand</option>
			<option value="Timor-leste">Timor-leste</option>
			<option value="Togo">Togo</option>
			<option value="Tokelau">Tokelau</option>
			<option value="Tonga">Tonga</option>
			<option value="Trinidad and Tobago">Trinidad and Tobago</option>
			<option value="Tunisia">Tunisia</option>
			<option value="Turkey">Turkey</option>
			<option value="Turkmenistan">Turkmenistan</option>
			<option value="Turks and Caicos Islands">Turks and Caicos Islands</option>
			<option value="Tuvalu">Tuvalu</option>
			<option value="Uganda">Uganda</option>
			<option value="Ukraine">Ukraine</option>
			<option value="United Arab Emirates">United Arab Emirates</option>
			<option value="United Kingdom">United Kingdom</option>
			<option value="United States">United States</option>
			<option value="United States Minor Outlying Islands">United States Minor Outlying Islands</option>
			<option value="Uruguay">Uruguay</option>
			<option value="Uzbekistan">Uzbekistan</option>
			<option value="Vanuatu">Vanuatu</option>
			<option value="Venezuela">Venezuela</option>
			<option value="Viet Nam">Viet Nam</option>
			<option value="Virgin Islands, British">Virgin Islands, British</option>
			<option value="Virgin Islands, U.S.">Virgin Islands, U.S.</option>
			<option value="Wallis and Futuna">Wallis and Futuna</option>
			<option value="Western Sahara">Western Sahara</option>
			<option value="Yemen">Yemen</option>
			<option value="Zambia">Zambia</option>
			<option value="Zimbabwe">Zimbabwe</option>
                     </select>
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Gender 
                  </td>
                  <td>
                     <input type="radio" name="gender" value="M" checked> M
                     <input type="radio" name="gender" value="F"> F
                  </td>
                  <td>
                   Birthdate 
                  </td>
                  <td>
                     <input type="text" name="birthdate" id="birthdate">
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Username
                  </td>
                  <td>
                     <input type="text" name="username" maxlength="10" size="10" />
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Password
                  </td>
                  <td>
                     <input type="password" name="password" id="pass1" maxlength="10" size="10" />
                  </td>
                  <td>
                    Confirm Password
                  </td>
                  <td>
                     <input type="password" name="confirmpassword" id="pass2" maxlength="10" size="10"  onkeyup="checkPass(); return false;" />
                     <span id="confirmMessage" class="confirmMessage"></span>
                  </td>
                  <td>
                    Change Password
                  </td>
                  <td>
                     <input type="radio" name="changepassword" value="y" checked> Yes 
                     <input type="radio" name="changepassword" value="n"> No 
                  </td>
                  </tr>
                  <tr>
                  <td>
                     Priviliges 
                  </td>
                  <td>
                     <select name="priviliges">
                        <option value="administrator">Administrator</option>
                        <option value="superUser">Super User</option>
                        <option value="familyMember">Family Member</option>
                        <option value="friend">Friend</option>
                        <option value="guest">Guest</option>
                        <option value="temporary">Temporary</option>
                     </select>
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Dropbox 
                  </td>
                  <td>
                     <input type="radio" name="dropbox" value="y" onclick="javascript:enabledropbox();" /> Yes
                     <input type="radio" name="dropbox" id="dropbox_no" value="n" onclick="javascript:disabledropbox();" checked> No
                  </td>
                  <td>
                    Dropbox Quota
                  </td>
                  <td>
                     <input type="text" name="dropboxquota" disabled maxlength="3" size="3"> 
                  <i>
                  MB
                  </i>
                  </td>
                  <tr>
                  <td>
                    Subscriber Active
                  </td>
                  <td>
                     <input type="radio" name="active" value="y" checked /> Yes
                     <input type="radio" name="active" value="n"> No
                  </td>
                  </tr>
                </table>
	</div>
	<div id="tabs-2">
		<p>
                 <table>  
                  <tr>
                  <td>
                    External Email 
                  </td>
                  <td>
                     <input type="text" name="extemail" maxlength="20" size="20" />
                  </td>
                  <td>
                     Verify External Email 
                  </td>
                  <td>
                     <input type="radio" name="extemailverified" value="y" /> Yes
                     <input type="radio" name="extemailverified" value="n" checked> No
                  </td>
                 </tr>
                 <tr>
                  <td>
                     Local Email
                  </td>
                  <td>
                     <input type="radio" name="locemailexist" value="y" /> Yes
                     <input type="radio" name="locemailexist" value="n" checked> No
                  </td>
                  </tr>
                  <tr>
                  <td>
                    User will have internal Phone 
                  </td>
                  <td>
                     <input type="radio" name="intphone" value="y" onclick="javascript:enableinternalphone();" /> Yes
                     <input type="radio" name="intphone" value="n" onclick="javascript:disableinternalphone();"checked> No
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Internal Phone Number 
                  </td>
                  <td>
                     <input type="text" name="intphonenum" disabled maxlength="9" size="9" />
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Call to Outside World 
                  </td>
                  <td>
                     <input type="radio" name="calloutside" id="calloutsideY" value="y" /> Yes
                     <input type="radio" name="calloutside" id="calloutsideN" value="n" checked> No
                  </td>
                  <td>
                    Call from Outside World
                  </td>
                  <td>
                     <input type="radio" name="recieveoutside" id="recieveoutsideY" value="y" /> Yes
                     <input type="radio" name="recieveoutside" id="recieveoutsideN" value="n" checked> No
                  </td>
                  </tr>
                  <tr>
                  <td>
                   Make Teleconference 
                  </td>
                  <td>
                     <input type="radio" name="teleconf" id="teleconfY" value="y" /> Yes
                     <input type="radio" name="teleconf" id="teleconfN" value="n" checked> No
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Make Webconference 
                  </td>
                  <td>
                     <input type="radio" name="webconf" value="y"  onclick="javascript:enablewebconf();" /> Yes
                     <input type="radio" name="webconf" value="n"  onclick="javascript:disablewebconf();" checked> No
                  </td>
                  <td>
                   Web Conference Quota
                  </td>
                  <td>
                     <input type="text" name="webconfquota" disabled maxlength="3" size="3" />
                  <i>
                  MB
                  </i>
                  </td>
                  </tr>
                  <tr>
                  <td>
                   Publish Website 
                  </td>
                  <td>
                     <input type="radio" name="webpublish" value="y"  onclick="javascript:enablewebpublish();" /> Yes
                     <input type="radio" name="webpublish" value="n"  onclick="javascript:disablewebpublish();" checked> No
                  </td>
                  <td>
                   Website Quota 
                  </td>
                  <td>
                     <input type="text" name="webpublishquota" disabled maxlength="3" size="3" />
                  <i>
                  MB
                  </i>
                  </td>
                  </tr>
                 </table>
                </p>
	</div>
	<div id="tabs-3">
		<p>
          <table>
                  <tr>
                  <td>
                    Job
                  </td>
                  <td>
                     <input type="text" name="Profession" maxlength="15" size="15" />
                  </td>
                  <tr>
                  <tr>
                  <td>
                    Mobile
                  </td>
                  <td>
                     <input type="text" name="mobile" maxlength="15" size="15" />
                  </td>
                  </tr>
                  <tr>
                  <td>
                   Phone 
                  </td>
                  <td>
                     <input type="text" name="phone" maxlength="15" size="15" />
                  </td>
                  </tr>
                  <tr>
                  <td>
                    Fax
                  </td>
                  <td>
                     <input type="text" name="fax" maxlength="15" size="15" />
                  </td>
                  </tr>
                  <tr>
                  <td>
                   Build CV 
                  </td>
                  <td>
                     <input type="radio" name="buildcv" value="y" /> Yes
                     <input type="radio" name="buildcv" value="n" checked> No
                  </td>
                  </tr>

          </table>
                </p>
	</div>
</div>

<div class="Create User">
    <center><input type="submit" value="Create Subscriber"></center>
</div>
</div>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:Ashraf Hassanein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39734643
replace :
	$(function() {
		$( "#tabs" ).tabs();
                $( "#birthdate" ).datepicker({changeMonth: true,changeYear: true});
		$( "#birthdate" ).datepicker( "option", "showAnim", "drop" );
                $( "#birthdate" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
                $( "input[type=submit], a, button" ).button().click(function( event ) {event.preventDefault();});
	});

Open in new window

by :
$(function() {
	$( "#tabs" ).tabs();
	$( "#birthdate" ).datepicker({changeMonth: true,changeYear: true});
	$( "#birthdate" ).datepicker( "option", "showAnim", "drop" );
	$( "#birthdate" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
	$( "a, button" ).button().click(function( event ) {
		event.preventDefault();
	});
	$("form[name='createuser']").submit(validateFormOnSubmit);
});

Open in new window

and replace :
		function validateFormOnSubmit(theForm) {
			var reason = "";

	  		reason += validateUsername(theForm.username);
  			reason += validatePassword(theForm.password);
  			reason += validateEmail(theForm.extemail);
 		 	reason += validatePhone(theForm.mobile);
 		 	reason += validatePhone(theForm.phone);
 		 	reason += validatePhone(theForm.fax);
  			reason += validateEmpty(theForm.from);

  			if (reason != "") {
    				alert("Some fields need correction:\n" + reason);
    				return false;
  			}
                        return true;
		}

Open in new window

by :
function validateFormOnSubmit(event) {
	var reason = "";
	
	reason += validateUsername(theForm.username);
	reason += validatePassword(theForm.password);
	reason += validateEmail(theForm.extemail);
	reason += validatePhone(theForm.mobile);
	reason += validatePhone(theForm.phone);
	reason += validatePhone(theForm.fax);
	reason += validateEmpty(theForm.from);
	
	if (reason != "") {
		event.preventDefault();
		alert("Some fields need correction:\n" + reason);
	}
}

Open in new window


to stop form submission and be able to see your error put the event.preventDefault(); in validateFormOnSubmit function just before var reason = "";
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39734645
additionaly take a look to :
http://jqueryvalidation.org/
http://demos.usejquery.com/ketchup-plugin/

and validate your page with this online tools :
http://validator.w3.org/
0
 

Author Comment

by:Ashraf Hassanein
ID: 39734672
Thank you so much I have some progress however now I have different issue:
The validateEmpty appears does not work at all, although other validation works.
The I can not see any alert or error message
The scripts after changes are:
        <script>
        $(function() {
                $( "#tabs" ).tabs();
                $( "#birthdate" ).datepicker({changeMonth: true,changeYear: true});
                $( "#birthdate" ).datepicker( "option", "showAnim", "drop" );
                $( "#birthdate" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
                $( "a, button" ).button().click(function( event ) {event.preventDefault();});
                $("form[name='createuser']").submit(validateFormOnSubmit);
        });

                function validateFormOnSubmit(theForm) {
                        event.preventDefault();
                        var reason = "";

                        reason += validateUsername(theForm.username);
                        reason += validatePassword(theForm.password);
                        reason += validateEmail(theForm.extemail);
                        reason += validatePhone(theForm.mobile);
                        reason += validatePhone(theForm.phone);
                        reason += validatePhone(theForm.fax);
                        reason += validateEmpty(theForm.from);

                        if (reason != "") {
                                event.preventDefault();
                                alert("Some fields need correction:\n" + reason);
                        }

                }

Open in new window


In the google chrome develover tools I get the following:
Uncaught TypeError: Object [object Object] has no method 'button' createuser.php:22
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
Uncaught TypeError: Cannot read property 'value' of undefined createuser.php:91
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 38

Expert Comment

by:Tom Beck
ID: 39735238
"button" is a function available in the jQuery UI. Most likely cause is that your jquery.ui javascript file is not loading properly or is otherwise corrupt. Check that this file is loading:

<script src="js/ui/jquery.ui.core.js"></script>

As a test, try replacing with this:

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39735242
replace :
$( "a, button" ).button().click(function( event ) {
by :
$( "a, button" ).click(function( event ) {
0
 

Author Closing Comment

by:Ashraf Hassanein
ID: 39737516
This was very helpful solution
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question