Web Analytics Made Easy -
StatCounter not allowing form submit if an error has occured! - CodingForum

Announcement

Collapse
No announcement yet.

not allowing form submit if an error has occured!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • not allowing form submit if an error has occured!

    below is my javascript for my form..it only allows users to input certain times

    Code:
    function limitText(limitField, limitNum) {
        if (limitField.value.length > limitNum) {
            limitField.value = limitField.value.substring(0, limitNum);
        } 
    }
    
      var divid = 'output';  
     var loadingmessage = 'Processing...';  
       
     function AJAX(){  
        
     var xmlHttp;  
      try{  
      xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari  
      return xmlHttp;  
      }  
      catch (e){  
      try{  
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer  
      return xmlHttp;  
      }  
      catch (e){  
      try{  
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  
      return xmlHttp;  
      }  
      catch (e){  
      alert("Your browser does not support AJAX!");  
      return false;  
      }  
      }  
      }  
        
     }  
        
      function formget(f, url) {  
        
      var poststr = getFormValues(f);  
      postData(url, poststr);  
        
      }  
        
      function postData(url, parameters){  
        
      var xmlHttp = AJAX();  
        
      xmlHttp.onreadystatechange =  function(){  
      if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){  
      document.getElementById(divid).innerHTML=loadingmessage;  
      }  
     if (xmlHttp.readyState == 4) {  
       
     document.getElementById(divid).innerHTML=xmlHttp.responseText;  
    
     }   
        
      }  
      xmlHttp.open("POST", url, true);  
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
     xmlHttp.setRequestHeader("Content-length", parameters.length);  
     xmlHttp.setRequestHeader("Connection", "close");  
       xmlHttp.send(parameters);  
     }  
        
     function getFormValues(fobj)  
        
     {  
      var str = "";  
      var valueArr = null;  
     var val = "";  
     var cmd = "";  
       
     for(var i = 0;i < fobj.elements.length;i++)  
       
     {  
    switch(fobj.elements[i].type)  
      
     {  
     case "text":  
       
     str += fobj.elements[i].name +  
     "=" + escape(fobj.elements[i].value) + "&";  
     break;  
      case "textarea":
    
                       str += fobj.elements[i].name +
                        "=" + escape(fobj.elements[i].value) + "&";
                        break;
     case "select-one":  
       
    str += fobj.elements[i].name +  
       "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";  
      break;  
       
      }  
     }  
       
     str = str.substr(0,(str.length - 1));  
     return str;  
       
     }  
       
     //-->
     
            function checkit(which) {
    var tim = which.value;
    var flag = 0;
    var flag2 = 0;
    if (!/^\d{1,2}[-:\.]\d{2}/.test(tim)) {  // basic format
    flag = 1;
    }
    tim = tim.replace(/[-\.]/g,":");
    tim = tim.split(/:/);
    if ((tim[0] > 23) || tim[1] > 59) {  // hour or minute values too great
    flag = 1;
    }
     
    if (!/^[0-5]?[0-9].[0-5]?[0-9]$/.test(tim))  // basic format
    flag2 = 1;
     
    if ( flag != 0 && flag2 != 0 ) {
    alert ("This field requires a valid time!" );
    which.value = "";
    which.focus();
    return false;
    }
    		}
    	function validateTime(nDistance){
    
    		var useDistance = nDistance.split("|");
    		var valid = true;
    		var nTimeField = document.forms[0]['userDistance'];
    		var nTime = document.forms[0]['userDistance'].value;
    		if (useDistance[0] == "withHours")
    				{
    				 if (nTime == "")
    					{
    					 return; 
    					}				
    				 if (!/^\d{1,2}[:\-]\d{1,2}[:\-]\d{1,2}$/.test(nTime)
    					 &&  !/^\d{1,2}[:\-]\d{1,2}[:\-]\d{1,2}\.\d{1,2}$/.test(nTime))
    					{
    					 alert('Hours, Minutes and Seconds must be separated\n' +
    						'by a : (colon) or, - (hyphen) \n' +
    						'Seconds may be expressed in tenths or hundreths:\n' +
    						'ss.ss or, s.ss or, s.s or, ss.s');	
    					 return;						 						
    					}
    				 nTime = nTime.replace(/[:\-]/g, "|").split("|");				 
    				 nTime[0] > 23 ? valid = false : 
    				 nTime[1] > 59 ? valid = false : 
    				 nTime[2] > 59.99 ? valid = false : null; 					 
    				 if (!valid)
    					{
    					 alert('Invalid time');
    					 nTimeField.value = "";
    					 nTimeField.focus();					 					 
    					}				
    				}
    			if (useDistance[0] == "withoutHours")
    				{	
    				 if (nTime == "")
    					{
    					 return; 
    					}				
    				 if (!/^\d{1,2}[:\-]\d{1,2}$/.test(nTime)
    					 &&  !/^\d{1,2}[:\-]\d{1,2}\.\d{1,2}$/.test(nTime))
    					{
    					 alert('Minutes and Seconds must be separated\n' +
    						'by a : (colon) or, - (hyphen) \n' +
    						'Seconds may be expressed in tenths or hundreths:\n' +
    						'ss.ss or, s.ss or, s.s or, ss.s');	
    					 return;						 						
    					}
    				 nTime = nTime.replace(/[:\-]/g, "|").split("|");
    				 nTime[0] > 59 ? valid = false : 
    				 nTime[1] > 59.99 ? valid = false : null; 					 
    				 if (!valid)
    					{
    					 alert('Invalid time');
    					 nTimeField.value = "";
    					 nTimeField.focus();					 
    					}
    				}				
    			if (useDistance[0] == "withoutMinutes")
    				{					 	
    				 if (nTime == "")
    					{
    					 return; 
    					}				
    				 if (!/^\d{1,2}$/.test(nTime)
    					 &&  !/^\d{1,2}\.\d{1,2}$/.test(nTime))
    					{
    					 alert('Seconds may be expressed in tenths or hundreths:\n' +
    						'ss.ss or, s.ss or, s.s or, ss.s');	
    					 return;						 						
    					}
    				 nTime = nTime.replace(/[:\-]/g, "|").split("|");
    				 nTime[0] > 59.99 ? valid = false : null; 					 
    				 if (!valid)
    					{
    					 alert('Invalid time');
    					 nTimeField.value = "";
    					 nTimeField.focus();					 
    					}
    				}	
    	}
    
    	function setDistance(nDistance){
    
    		var useDistance = nDistance.split("|");
    		var nTimeField = document.forms[0]['userDistance'];
    		nTimeField.value = "";
    		document.forms[0]['userDistance'].parentNode.firstChild.data = useDistance[1] + ": ";		
    		if (useDistance[0] == "#")
    			{		
    			 nTimeField.readOnly = true;			 
    			 return;
    			}
    		nTimeField.readOnly = false; 					
    	}
    
    	function init(){
    
    		document.forms[0]['userDistance'].onblur = function()
    			{
    			 validateTime(document.forms[0]['distance'].value);
    			}		
    	}
    
    	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);
    the error message comes up put once you click ok you can still submitt the text even if its forbidden.... any way around this, such as deleting the text within the textbox when the error box is shown??

  • #2
    Where do you call the validation function? On using which event? Can we see the HTML as well (the form and the submit button, if any)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      yes sure....its all here

      Code:
      <?php 
      ob_start("ob_gzhandler");
      session_start();
      error_reporting(E_ALL); 
      require_once '../settings.php';?>
      <link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
      <SCRIPT LANGUAGE="JavaScript" SRC="http://www.runningprofiles.com/members/include/formsubmit.js">
      </SCRIPT>
      <?php include "../info.php"; // sets username/id ect
      include "../getuser.php"; // records user view on page
      $result = mysql_query("SELECT * FROM pb") or die(mysql_error()); 
      //now display all data
       ?>  <fieldset>
                      <legend>My pbs</legend>
      <div align="center"><br />
           <?php
      $result = mysql_query("SELECT * FROM pb Order by uid,distance") or die(mysql_error());
       
      echo "<table class=\"profilepbs\" width=195 align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
         <tr><td width=105 bgcolor=\"#FFFFFF\"><div align=\"center\">Distance</div></td><td width=125 bgcolor=\"#FFFFFF\"><div align=\"center\">Pb</div></td></tr>
         <tr>
           <td height=\"1\" colspan=\"2\" bgcolor=\"#000000\"></td>
           <td height=\"1\" colspan=\"2\" bgcolor=\"#000000\"></td>
         </tr>
         ";
      while ( $row = mysql_fetch_array($result) ) {echo "<tr><td><center>" . $row["distance"] . "</center></td><td><center>" . $row["pb"] . "</center></td></tr>";
        }
      echo "</table>
       <p>";
       ?>
       
       </p>
      </div>
      <form id="submit">
        <div align="center">
          <p><br>
            Add a new personal best (Enter pb as 2:01.1 or 13:25.3 ect): oh any your id is <?php echo $id;?>    </p>
          <table>
            <tr>
        <td></td><td> <select name="distance" onchange="setDistance(this.value)">
      					<option value="#|Distance">Select the distance</option>
      					<option value='withoutMinutes|100m' >100m</option>
                          <option value='withoutMinutes|200m'>200m</option>
                          <option value='withoutMinutes|400m'>400m</option>
                          <option value='withoutHours|800m'>800m</option>
                          <option value='withoutHours|1500m'>1500m</option>
                          <option value='withoutHours|3000m'>3000m</option>
                          <option value='withoutHours|5000m'>5000m</option>
                          <option value='withoutHours|10,000m'>10,000m</option>
                          <option value='withoutHours|5k Road'>5k Road</option>
                          <option value='withoutHours|10k Road'>10k Road</option>
                          <option value='withHours|H Marathon'>H Marathon</option>
                           <option value='withHours|Marathon'>Marathon</option>
                            <option value='withoutHours|3k S/Chase'>3k S/Chase</option>
                             <option value='withoutMinutes|110m Hurdles'>110m Hurdles</option>
                              <option value='withoutMinutes|400m Hurdles'>400m Hurdles</option>
                                <option value='withoutMinutes|60m (I)'>60m (I)</option>
                                  <option value='withoutMinutes|60m Hurdles'>60m Hurdles</option>
      				</select>
      
      				<label>Distance:&nbsp;<input type="text" name="userDistance" size="10" class="timeData" readonly></label>	
      </td>
        <td>
          
        </td>
        </tr>
          </table>
          <p><input type="button" name="Submit" value="OK" onClick="javascript: formget(this.form, 'http://www.runningprofiles.com/do5.php');"> 
          </p>
        </div>
         <div align="center">
         <div id="output" style="color: blue;">   </div>  </div>
      </form>
       </fieldset>

      Comment


      • #4
        I notice one problem:-

        In Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID. You should also avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'text' or 'checked' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'. And of course you may not give a variable a name which is a Javascript keyword or event such as alert, case, char, false, int, onload, return, this, void, and so on.

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          i chnaged values to submitpbs but obvisuly this is not in the javascript and so the error still occures

          Comment

          Working...
          X