SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onsubmit/onclick unable to find function.

    Help!

    Hi all,

    For the first time in a while, i'm stumped.

    I've created a simple validation script that returns true or false. I've then attached this to a form's onsubmit event.

    When I submit the form, it isn't able to find the function. Even if I move it to the submit button using the onclick, it comes back with the same error.

    But!! If I put in the address bar:

    javascript:alert(validate()) it alerts the word false and Firebug appears with the breakpoint I set.

    Am I being simple here and missing something?

    The only difference is that i'm using SSL on this page. I've not really used SSL before.

    Any idea's?

    Cheers


  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's the error message? Function not defined? That doesn't necessarily mean it can't find the function. It could mean there's a coding error in the function.

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've done the same thing I do every time,

    Code xhtml:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="https://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript" src="spt_paymentupgrade.js"></script>
        </head>
        <body>
     
            <h2>Become a <strong>Premium</strong> User</h2>
     
     
    	<form method="post" onsubmit="return validate();">
     
    		<table width="100%">
    			<tr>
    				<td>Forename: </td>
    				<td><input type="text" id="input_forename" name="forename" /></td>
    			</tr>
    			<tr>
    				<td>Surname: </td>
    				<td><input type="text" id="input_surname" name="surname" /></td>
    			</tr>
    			<tr>
    				<td>Payment Term: (payment every)</td>
    				<td><select name="term"><option value="1">1 Month - $4.99</option><option value="12">1 Year - $49.99</option></select>
    				</td>
    			</tr>
    			<tr>
    				<td>Card Number: </td>
    				<td><input type="text" id="input_cardno" name="number" /></td>
    			</tr>
    			<tr>
    				<td>Card Expiration: </td>
    				<td>Month: 
    					<select name="month" id="input_month">
    						<option value="01">01</option>
    						<option value="02">02</option>
    						<option value="03">03</option>
    						<option value="04">04</option>
    						<option value="05">05</option>
    						<option value="06">06</option>
    						<option value="07">07</option>
    						<option value="08">08</option>
    						<option value="09">09</option>
    						<option value="10">10</option>
    						<option value="11">11</option>
    						<option value="12">12</option>
    					</select> Year: 
    					<select name="year" id="input_year">
    												<option value="2008">2008</option>
    												<option value="2008">2008</option>
    												<option value="2009">2009</option>
    												<option value="2010">2010</option>
    												<option value="2011">2011</option>
    												<option value="2012">2012</option>
    												<option value="2013">2013</option>
    											</select>
    				</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td><input type="submit" name="validate" value="Go" /></td>
    			</tr>
    		</table>
     
    	</form>
        </body>
    </html>

    and in the js file:

    Code javascript:
    function validate() {
     
    	var fn = document.getElementById('input_forename');
    	var sn = document.getElementById('input_surname');
    	var cn = document.getElementById('input_cardno');
    	var im = document.getElementById('input_month');
    	var iy = document.getElementById('input_year');
     
    	var errors = new Array();
     
    	if(fn.value.length == 0)
    		errors[errors.length] = 'Forename';
    	if(sn.value.length == 0)
    		errors[errors.length] = 'Surname';
    	if(cn.value.length == 0 || cn.value.length < 13 || cn.value.length > 16)
    		errors[errors.length] = 'Credit Card';
     
    	if(errors.length == 0)
    		return true;
    	var str = 'The following fields must be corrected:\n\n';
    	for(var error in errors)
    		str += error + '\n';
     
    	return false;
    }

    As I said, I can see the code via Firebug and when calling the code by typing the following in the address bar it works.

    Code javascript:
    javascript:alert(validate());

    Gav


  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that validate is referring to your submit button - try changing the name of the submit button or the name of the function and it should work

  5. #5
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that worked wonders.. Typical lol

    Thank you!



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •