JavaScript - Javascript Validation Not Stopping Submitting
I am using the following validation routine
function savenotes() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var result = xmlhttp.responseText.split("~"); alert(result[0]) if (result[0] == "1") { // alert(result[1]); alert("should return true"); valid = true; } else { alert(result[1]); valid = false; } } } var str = "ok"; var mypost = "check.asp" // alert(mypost); xmlhttp.open("GET", mypost, true); xmlhttp.send(); return valid; } now where do I put the return so that it returns to my form if it's valid or not. It doesn't seem to work to me and the form is always submitted I am calling this with <form method=post action=admin.asp onsubmit="return savenotes()"> Similar TutorialsI have a simple form that works via a suggested results system (http://www.brandspankingnew.net/arch...uggest_v2.html) While in firefox the go button is greyed out and enter doesn't submit the form in IE pressing the enter key submits the form. Is there any way with a bit of javascript to stop IE from doing a form submission on enter but only for this form (since there are many more on the site) PHP Code: <form action="index.php" method="GET" name="qsform" id="qsform"><input type="hidden" name="p" id="p" value="compdetails" style="border:1px solid #000000;" /><input type="text" name="quicksearch" id="quicksearch" value="Find Company" style="border:1px solid #000000;" onFocus="clearText(this)" onBlur="clearText(this)" /><input type="hidden" name="cid" id="cid" value="" style="border:1px solid #000000;" /> <input type="submit" name="go" id="go" value="Go" disabled="true" /></form> The thing i want to do If any field is empty -->User will see a message to fill out the field When both field is filled out and press Submit button --> goes to thankyou.php page <html> <head></head> <body> <?php $fname = $_POST['fname']; $lname = $_POST['lname']; ?> <form action="thankyou.php" method="POST"> First Name: <input type="text" name='fname'/> <?php if(empty($fname)) echo "Please provide your First Name";?><br/> Last Name: <input type="text" name='lname'/> <?php if(empty($lname)) echo "Please provide your Last Name";?><br/> <input type="submit" value="submit" /><input type="reset" value="Clear Form"/> </form> </body> </html> thankyou.php <?php echo "Thank you $fname $lname for your time." ?> I am having trouble submitting a form after validation. Here is my script. The validation works fine its just that after the form does not submit. Code: <script language='javascript'> function verifyMe(){ var msg=''; if(document.getElementById('company').value==''){ msg+='- Company Name\n\n';} if(document.getElementById('contact').value==''){ msg+='- Contact Name / Job Title\n\n';} if(document.getElementById('address').value==''){ msg+='- Trading Address\n\n';} if(document.getElementById('postcode').value==''){ msg+='- Postcode\n\n';} if(document.getElementById('telephone').value==''){ msg+='- Telephone\n\n';} if(document.getElementById('mobile').value==''){ msg+='- Mobile\n\n';} var email=document.getElementById('email').value; if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){ msg+='- Invalid Email Address: '+email+'\n\n';} if(document.getElementById('email').value==''){ msg+='- Email\n\n';} if(document.getElementById('employees').value==''){ msg+='- Number of Full-Time equivalent employees\n\n';} if(document.getElementById('turnover').value==''){ msg+='- Annual Turnover\n\n';} if(document.getElementById('assetvalue').value==''){ msg+='- Total asset value\n\n';} if(!document.getElementById('sme[0]').checked && !document.getElementById('sme[1]').checked){ msg+='- Is more than 25% of your company owned by an enterprise which is not itself an SME?\n\n';} if(document.getElementById('tradetime').value==''){ msg+='- How many months has your company been trading?\n\n';} if(document.getElementById('nature').value==''){ msg+='- Nature of Business Sector\n\n';} if(document.getElementById('typeofo').value==''){ msg+='- Type of Organisation\n\n';} if(document.getElementById('companyregisteredname').value==''){ msg+='- Company Registered Name\n\n';} if(document.getElementById('regnumber').value==''){ msg+='- Registration Number\n\n';} if(document.getElementById('vatreg').value==''){ msg+='- VAT Reg Number\n\n';} if(document.getElementById('regaddress').value==''){ msg+='- Registered Address\n\n';} if(document.getElementById('catapply').value==''){ msg+='- Which Catagories Apply?\n\n';} if(document.getElementById('fuelused').value==''){ msg+='- Current fuel used\n\n';} if(document.getElementById('otherfuel').value==''){ msg+='- Other (if applicable)\n\n';} if(document.getElementById('heatingpercent').value==''){ msg+='- What percentage of this is used for heating?\n\n';} if(document.getElementById('factoryspaceheating').value==''){ msg+='- Factory Space Heating\n\n';} if(document.getElementById('officeheating').value==''){ msg+='- Office Heating\n\n';} if(document.getElementById('hotwater').value==''){ msg+='- Hot Water\n\n';} if(document.getElementById('heaterrating').value==''){ msg+='- Combine Rating of Existing Heaters\n\n';} if(document.getElementById('existingheaters').value==''){ msg+='- Number of existing heaters\n\n';} if(document.getElementById('setbackheating').value==''){ msg+='- Is night set back heating employed?\n\n';} if(document.getElementById('billavalable').value==''){ msg+='- Are they avalable?\n\n';} if(document.getElementById('factorysize').value==''){ msg+='- Factory Size\n\n';} if(document.getElementById('roofheighte').value==''){ msg+='- Roof Height to Eaves\n\n';} if(document.getElementById('roofheightr').value==''){ msg+='- Roof Height to Ridge\n\n';} if(document.getElementById('builddate').value==''){ msg+='- Approx. Date building was built\n\n';} if(document.getElementById('spraybooths').value==''){ msg+='- Spray Booths?\n\n';} if(document.getElementById('dustextract').value==''){ msg+='- Dust Extraction\n\n';} if(document.getElementById('air').value==''){ msg+='- Is the air returned to the building?\n\n';} if(document.getElementById('rollershutters').value==''){ msg+='- Roller Shutters?\n\n';} if(document.getElementById('weekoc').value==''){ msg+='- Weekly building occupancy\n\n';} if(document.getElementById('occupancy').value==''){ msg+='- Daily Building Occupancy\n\n';} if(document.getElementById('woodwaste').value==''){ msg+='- Type of wood waste\n\n';} if(document.getElementById('woodavailable').value==''){ msg+='- Quantity available per wek\n\n';} if(document.getElementById('upload').value==''){ msg+='- Upload bills here.\n\n';} if(msg!=''){ alert('The following fields are empty or invalid:\n\n'+msg); return false }else{ document.carbonform.submit(); return true } } </script> <html> <head> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="stylized" class="myform"> <form name='carbonform' action="process.php" method='POST' enctype='application/x-www-form-urlencoded' onsubmit='return verifyMe();'> <table class='table_form_1' id='table_form_1' cellspacing='0'> <h1>Carbon Trust Application Form.</h1> <p>Please make sure you enter all fields marked with an <b style='color:red'>*</b>. If you submit an incomplete form we cannot help you.</p> <tr> <td class='ftbl_row_1' ><LABEL for='company' ACCESSKEY='none' ><b style='color:red'>*</b>Company Name </td> <td class='ftbl_row_1a' ><input type='text' name='company' id='company' size='45' maxlength='150' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='contact' ACCESSKEY='none' ><b style='color:red'>*</b>Contact Name / Job Title </td> <td class='ftbl_row_2a' ><input type='text' name='contact' id='contact' size='45' maxlength='150' value=''> </td> </tr> <tr> <td valign='top' class='ftbl_row_1' ><LABEL for='address' ACCESSKEY='none' ><b style='color:red'>*</b>Trading Address </td> <td class='ftbl_row_1a' ><textarea name='address' id='address' cols='35' rows='6' value=''></textarea> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='postcode' ACCESSKEY='none' ><b style='color:red'>*</b>Postcode </td> <td class='ftbl_row_2a' ><input type='text' name='postcode' id='postcode' size='45' maxlength='10' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='telephone' ACCESSKEY='none' ><b style='color:red'>*</b>Telephone </td> <td class='ftbl_row_1a' ><input type='text' name='telephone' id='telephone' size='45' maxlength='30' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='mobile' ACCESSKEY='none' ><b style='color:red'>*</b>Mobile </td> <td class='ftbl_row_2a' ><input type='text' name='mobile' id='mobile' size='45' maxlength='20' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='email' ACCESSKEY='none' ><b style='color:red'>*</b>Email </td> <td class='ftbl_row_1a' ><input type='text' name='email' id='email' size='45' maxlength='50' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='employees' ACCESSKEY='none' ><b style='color:red'>*</b>Number of Full-Time equivalent employees </td> <td class='ftbl_row_2a' ><input type='text' name='employees' id='employees' size='45' maxlength='8' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='turnover' ACCESSKEY='none' ><b style='color:red'>*</b>Annual Turnover </td> <td class='ftbl_row_1a' ><input type='text' name='turnover' id='turnover' size='45' maxlength='20' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='assetvalue' ACCESSKEY='none' ><b style='color:red'>*</b>Total asset value </td> <td class='ftbl_row_2a' ><input type='text' name='assetvalue' id='assetvalue' size='45' maxlength='20' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><b style='color:red'>*</b>Is more than 25% of your company owned by an enterprise which is not itself an SME? </td> <td class='ftbl_row_1a' > <LABEL ACCESSKEY='1'><input type='checkbox' name='sme' id='sme[0]' value='Yes'>Yes</LABEL> <LABEL ACCESSKEY='2'><input type='checkbox' name='sme' id='sme[1]' value='No' CHECKED >No</LABEL> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='tradetime' ACCESSKEY='none' ><b style='color:red'>*</b>How many months has your company been trading? </td> <td class='ftbl_row_2a' ><input type='text' name='tradetime' id='tradetime' size='45' maxlength='5' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='nature' ACCESSKEY='none' ><b style='color:red'>*</b>Nature of Business Sector </td> <td class='ftbl_row_1a' ><input type='text' name='nature' id='nature' size='45' maxlength='50' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='typeofo' ACCESSKEY='none' ><b style='color:red'>*</b>Type of Organisation </td> <td class='ftbl_row_2a' ><select name='typeofo' id='typeofo'> <option value='Public'>Public</option> <option value='Private'>Private</option> <option value='Charity'>Charity</option> <option value='Limited / Sole Trader'>Limited / Sole Trader</option> </select> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='companyregisteredname' ACCESSKEY='none' ><b style='color:red'>*</b>Company Registered Name </td> <td class='ftbl_row_1a' ><input type='text' name='companyregisteredname' id='companyregisteredname' size='45' maxlength='150' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='regnumber' ACCESSKEY='none' ><b style='color:red'>*</b>Registration Number </td> <td class='ftbl_row_2a' ><input type='text' name='regnumber' id='regnumber' size='45' maxlength='10' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='vatreg' ACCESSKEY='none' ><b style='color:red'>*</b>VAT Reg Number </td> <td class='ftbl_row_1a' ><input type='text' name='vatreg' id='vatreg' size='45' maxlength='15' value=''> </td> </tr> <tr> <td valign='top' class='ftbl_row_2' ><LABEL for='regaddress' ACCESSKEY='none' ><b style='color:red'>*</b>Registered Address </td> <td class='ftbl_row_2a' ><textarea name='regaddress' id='regaddress' cols='35' rows='6' value=''></textarea> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='catapply' ACCESSKEY='none' ><b style='color:red'>*</b>Which Catagories Apply? </td> <td class='ftbl_row_1a' ><select name='catapply' id='catapply'> <option value='Space Heating'>Space Heating</option> <option value=' Process Heating'> Process Heating</option> </select> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='fuelused' ACCESSKEY='none' ><b style='color:red'>*</b>Current fuel used </td> <td class='ftbl_row_2a' ><select name='fuelused' id='fuelused'> <option value='Gas'>Gas</option> <option value='Oil'>Oil</option> <option value='Other'>Other</option> </select> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='otherfuel' ACCESSKEY='none' ><b style='color:red'>*</b>Other (if applicable) </td> <td class='ftbl_row_1a' ><input type='text' name='otherfuel' id='otherfuel' size='45' maxlength='30' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='heatingpercent' ACCESSKEY='none' ><b style='color:red'>*</b>What percentage of this is used for heating? </td> <td class='ftbl_row_2a' ><input type='text' name='heatingpercent' id='heatingpercent' size='45' maxlength='3' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='factoryspaceheating' ACCESSKEY='none' ><b style='color:red'>*</b>Factory Space Heating </td> <td class='ftbl_row_1a' ><input type='text' name='factoryspaceheating' id='factoryspaceheating' size='45' maxlength='100' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='officeheating' ACCESSKEY='none' ><b style='color:red'>*</b>Office Heating </td> <td class='ftbl_row_2a' ><input type='text' name='officeheating' id='officeheating' size='45' maxlength='3' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='hotwater' ACCESSKEY='none' ><b style='color:red'>*</b>Hot Water </td> <td class='ftbl_row_1a' ><input type='text' name='hotwater' id='hotwater' size='45' maxlength='3' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='heaterrating' ACCESSKEY='none' ><b style='color:red'>*</b>Combine Rating of Existing Heaters </td> <td class='ftbl_row_2a' ><input type='text' name='heaterrating' id='heaterrating' size='45' maxlength='10' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='existingheaters' ACCESSKEY='none' ><b style='color:red'>*</b>Number of existing heaters </td> <td class='ftbl_row_1a' ><input type='text' name='existingheaters' id='existingheaters' size='45' maxlength='4' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='setbackheating' ACCESSKEY='none' ><b style='color:red'>*</b>Is night set back heating employed? </td> <td class='ftbl_row_2a' ><select name='setbackheating' id='setbackheating'> <option value='Yes'>Yes</option> <option value='No'>No</option> </select> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='billavalable' ACCESSKEY='none' ><b style='color:red'>*</b>Are they avalable? </td> <td class='ftbl_row_1a' ><select name='billavalable' id='billavalable'> <option value='Yes'>Yes</option> <option value='No'>No</option> </select> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='factorysize' ACCESSKEY='none' ><b style='color:red'>*</b>Factory Size </td> <td class='ftbl_row_2a' ><input type='text' name='factorysize' id='factorysize' size='45' maxlength='60' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='roofheighte' ACCESSKEY='none' ><b style='color:red'>*</b>Roof Height to Eaves </td> <td class='ftbl_row_1a' ><input type='text' name='roofheighte' id='roofheighte' size='45' maxlength='10' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='roofheightr' ACCESSKEY='none' ><b style='color:red'>*</b>Roof Height to Ridge </td> <td class='ftbl_row_2a' ><input type='text' name='roofheightr' id='roofheightr' size='45' maxlength='10' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='builddate' ACCESSKEY='none' ><b style='color:red'>*</b>Approx. Date building was built </td> <td class='ftbl_row_1a' ><input type='text' name='builddate' id='builddate' size='45' maxlength='10' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='spraybooths' ACCESSKEY='none' ><b style='color:red'>*</b>Spray Booths? </td> <td class='ftbl_row_2a' ><select name='spraybooths' id='spraybooths'> <option value='Yes'>Yes</option> <option value='No'>No</option> </select> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='dustextract' ACCESSKEY='none' ><b style='color:red'>*</b>Dust Extraction </td> <td class='ftbl_row_1a' ><select name='dustextract' id='dustextract'> <option value='Yes'>Yes</option> <option value='No'>No</option> </select> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='air' ACCESSKEY='none' ><b style='color:red'>*</b>Is the air returned to the building? </td> <td class='ftbl_row_2a' ><select name='air' id='air'> <option value='Yes'>Yes</option> <option value='No'>No</option> </select> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='rollershutters' ACCESSKEY='none' ><b style='color:red'>*</b>Roller Shutters? </td> <td class='ftbl_row_1a' ><select name='rollershutters' id='rollershutters'> <option value='Yes'>Yes</option> <option value='No'>No</option> </select> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='weekoc' ACCESSKEY='none' ><b style='color:red'>*</b>Weekly building occupancy </td> <td class='ftbl_row_2a' ><input type='text' name='weekoc' id='weekoc' size='45' maxlength='10' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='occupancy' ACCESSKEY='none' ><b style='color:red'>*</b>Daily Building Occupancy </td> <td class='ftbl_row_1a' ><input type='text' name='occupancy' id='occupancy' size='45' maxlength='5' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='woodwaste' ACCESSKEY='none' ><b style='color:red'>*</b>Type of wood waste </td> <td class='ftbl_row_2a' ><input type='text' name='woodwaste' id='woodwaste' size='45' maxlength='30' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='woodavailable' ACCESSKEY='none' ><b style='color:red'>*</b>Quantity available per wek </td> <td class='ftbl_row_1a' ><input type='text' name='woodavailable' id='woodavailable' size='45' maxlength='20' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='upload' ACCESSKEY='none' ><b style='color:red'>*</b>Upload bills here. </td> <td class='ftbl_row_2a' ><input type='file' name='upload' id='upload' size='100' value=''> </td> </tr> <tr> <td colspan='2' align='right'><input type='submit' name='submit' value='Submit' onclick="function verifyMe()" > <input type='reset' name='reset' value='Reset'><br /><a href='http://www.Jaynetdesign.com.com' style='font-family:arial;font-size:10px;color:#C0C0C0;text-decoration:none'>Created by James Eley</a> </td> </tr> </table> </form> </div> </body> </HTML> Key info is on lines: 138 - 152 Code: if(msg!=''){ alert('The following fields are empty or invalid:\n\n'+msg); return false }else{ document.carbonform.submit(); return true } } Code: <form name='carbonform' action="process.php" method='POST' enctype='application/x-www-form-urlencoded' onsubmit='return verifyMe();'> and 441. Code: <td colspan='2' align='right'><input type='submit' name='submit' value='Submit' onclick="function verifyMe()" > <input type='reset' name='reset' value='Reset'> My php script just contains an echo to test. Thanks in advance. I have a problem when submitting through javascript, i have used it hundred times, but i dont know why it refuses to work now, http://two.xthost.info/shailesh/test.htm Please look at the attached file in IE 6 and when you click on "Delete Selected" button, you get javascript error and the form refuses to submit the error is on line document.myform.submit(); I wonder if someone can help me. I've recently put together a new web form. The form is validated using JavaScript - it shows real-time error messages when forms are left blank, and also prevents the form from being submitted if any of the input data is invalid. The problem I have is that if JavaScript is disabled, the form cannot be submitted. At the moment I am submitting the form using the following button Code: <input type="button" value="Next" onclick="submitForm(this.form);" id="button" /> The 'onclick' event runs the 'submitForm' function, which runs a final validation check and submits the form if everything checks out. If there is invalid data in the table, an alert box pops up which prevents the form from submitting. Unfortunately, if JavaScript is disabled, the 'Next' button does nothing, as it is relying on JS to submit the form. I have tried using <input type="submit"> as well as <button type="submit">. Both of these allow the form to be submitted if JS is disabled. Unfortunately, when JavaScript IS enabled, the validation gets bypassed - the alert box still shows up but once you click 'OK' the form gets processed anyway. Can anyone suggest a solution? I am planning to write server-side validation in PHP as a backup but would prefer it if my JavaScript validation would work in it's entirety and the PHP simply be in place as a last line of defence for when JS is disabled. Thanks! I have created a small file where I check if the passwords entered in the code match or not, if they do not match, the button that links to the next webpage should not work(allow them to go to the next wepage). ive tried various websites to help me try and solve this but I just dodont understand what they are doing. I'm very new to programming btw here is some example code: Code: !doctypeHTML <html> <script> //lets just pretend that ive already done all the .getelement by id stuff to get the code things //suppose the person did not write anything in the form, an error will get pushed(alerted basically) if(!form['password'].value) error.push('Missing Password'); <form> <label>Password<br><input type="password" name="password"></label> <label>Confirm Password<br><input type="password" name="confirm"</label> </form> </html> now how would I make the submit button not submit if there are errors? please be as simple as possible, I am new to this! Consider the following piece of code: PHP Code: <form method='post' name='myform'> <input type='submit' name='sub' value='submit 1' /> <input type='submit' name='sub' value='submit 2' /> <a href='javascript:document.myform.submit();'>submit 3</a> </form> <?php echo "You clicked ".$_POST["sub"]; ?> So if you click "submit 1", the output will be You clicked submit 1. And if you click "submit 2", the output will be You clicked submit 2. That's obvious. But ... what do I need to do to pass the key/value pair "sub" = "submit 3" to the server via POST when I click on the hyperlink showing "submit 3" - so that the output is You clicked submit 3 (i.e. so that $_POST contains "submit 3" referenced to index "sub"). I've tried <a href='javascript: a = {"x" : 1}; document.myform.submit(a);'>submit 3</a>, but that doesn't cause a to be passed via POST. Hello, I have the following DIV tag Code: <div id = "cbContainer"> </div> The div is initially hidden as the display is set to none. I have one text input in my form. Depending upon the changed text in that textbox, there is a javascript function which creates a list of checkboxes and by using the innerHTML, the above div is filled up with the check boxes like shown in the code below: Code: var chkboxes = ''; for(var r = 0; r < related.length; r++) { chkboxes = chkboxes + '<div class ="versionswidth">'+ '<input type="checkbox" name="version" value = related[r] />'+ related[r] + '</div>'; } var div = document.getElementById("cbContainer"); div.innerHTML = chkboxes; div display is then set as "block" which makes the div visible. The problem is that on form submission, the input of selected checkboxes is not submitting. I will be grateful for any help. Thank you. Code: // Values for fading var fadeBy = 10; //Amount to fade by var fadeTime = 120; //Time in milliseconds to fade. var ie_counter = 0; //Counter for crappy IE. var fade_to = 70; function fader(obj) { var div = document.getElementById(obj); //Object details var opacity = div.style.opacity * 100 + fadeBy; //Fade (FF) ie_counter = ie_counter + fadeBy; var opacity_ie = div.style.filter = "alpha(opacity=" + ie_counter + ")"; //Fade (IE) div.style.opacity = opacity / 100; if ( ie_counter != fade_to || opacity != fade_to ) { setTimeout( "fader('" + obj + "')", fadeTime ); } } The loop stops for firefox (good browser), but not for internet explorer for some reason :/ Apart for the fact that IE is crap why is this? as I have no idea if this will boil down to a JS problem, or a CSS problem, I've posted this to both forums. If some enterprising moderator would like to consolidate them, have at ye! Using javascript I've made a simple drag and drop interface for a backend component. Elements are dragged from a list at right into a main area. The elements are organized as a list (<ul><li>). Each has a mouse down listener which flicks on the visibility of a div (that mirrors the appearance of the named <li>) and then follows the mouse as long as it is needed. All is peachy... ... BUT THEN !!! ... As one might predict, the dragging action selects whatever content comes between the list item where the drag started and the placement where the drag ends. It's ugly and distracting; furthermore it changes my nice 'pointer' cursor to a 'text' cursor. I don't want to have to disable selecting for the entire page (nor do I know of a slick, elegant way to accomplish this) and I've tried all sorts of other hackery: cover up divs were especially fail-tastic. Hoping someone out there's cleverness gland can help me with this one. Excerpt from the site below to give an idea of layout: elements of the orange list at right get dragged into the main area in the middle I can't link to the site directly, sorry, I don't have a version ready that doesn't modify existing databases. Thanks, -S as I have no idea if this will boil down to a JS problem, or a CSS problem, I've posted this to both forums. If some enterprising moderator would like to consolidate them, have at ye! CSS thread link Hi, validation for pin must not be allow given below conditions 6 rising consecutive numbers(e.g 123456,456789 etc) 6 Descending consecutive numbers (eg. 654321,987654) All same digit (eg 111111,222222) Same digit appearing 3 or more time in pin code (eg:121212,1111432) for some reason this is not working can anyone tell me why? Code: <script language="JavaScript" type="text/javascript"> function swap(nam,im) { eval("document." + nam + ".src=" + im + ".src"); } van_b = new Image; van_b.src = "images/bus_menu2/busmenub_Passvan01on.jpg"; van_a = new Image; van_a.src = "images/bus_menu2/busmenub_Passvan01off.jpg"; excoach_b = new Image; excoach_b.src = "images/bus_menu2/busmenuB_Excoach01on.jpg"; excoach_a = new Image; excoach_a.src = "images/bus_menu2/busmenuB_Excoach01off.jpg"; specialcoach_b = new Image; specialcoach_b.src = "images/bus_menu/busmenu_specialtycoach01on.jpg"; specialcoach_a = new Image; specialcoach_a.src = "images/bus_menu/busmenu_specialtycoach01off.jpg"; minicoach_b = new Image; minicoach_b.src = "images/bus_menu2/busmenuB_minicoach01on.jpg"; minicoach_a = new Image; minicoach_a.src = "images/bus_menu2/busmenuB_minicoach01off.jpg"; motorcoach_b = new Image; motorcoach_b.src = "images/bus_menu2/busmenub_motorcoach01on.jpg"; motorcoach_a = new Image; motorcoach_a.src = "images/bus_menu2/busmenub_motorcoach01off.jpg"; function checkForm() { frm = window.document.contactForm; if (frm.pickupmonth.value =='') { alert('Please enter valid pickup Month'); frm.pickupmonth.focus(); return false; } if(frm.pickupday.value== "") { alert('Please enter pickup Day'); frm.pickupday.focus(); return false; } if(frm.pickupyear.value == "") { alert('Please enter pickup Year'); frm.pickupyear.focus(); return false; } if(frm.pickupaddress.value == "") { alert('Please enter pickup Address'); frm.pickupaddress.focus(); return false; } if (frm.pickupcity.value== "") { alert('Please enter pickup City'); frm.pickupcity.focus(); return false; } if (!validateInt(frm.pickupzip.value)) { if(frm.pickupzip.value =="" ) alert('Please enter pickup Zip Code'); frm.pickupzip.value=""; frm.pickupzip.focus(); return false; } if(frm.pickuphour.value == "") { alert('Please enter pickup Hour'); frm.pickuphour.focus(); return false; } if(frm.pickupminute.value == "") { alert('Please enter pickup Minute'); frm.pickupminute.focus(); return false; } if(frm.pickupam.value == "") { alert('Please enter pickup AM or PM'); frm.pickupam.focus(); return false; } if(frm.dropoffaddress.value == "") { alert('Please enter dropoff Address'); frm.dropoffaddress.focus(); return false; } if(frm.dropoffcity.value == "") { alert('Please enter dropoff City'); frm.dropoffcity.focus(); return false; } if (!validateInt(frm.dropoffpzip.value)) { if(frm.dropoffpzip.value =="" ) alert('Please enter a valid dropoff Zip Code'); frm.dropoffpzip.value=""; frm.dropoffpzip.focus(); return false; } if(frm.arrivalhour.value == "") { alert('Please enter arrival Hour'); contactForm.arrivalhour.focus(); return false; } if(frm.arrivalmin.value == "") { alert('Please enter arrival Minute'); frm.arrivalmin.focus(); return false; } if(frm.arrivalam.value == "") { alert('Please enter arrival AM or PM'); frm.arrivalam.focus(); return false; } if(frm.vehicle.value == "") { alert('Please enter Vehicle Type'); frm.vehicle.focus(); return false; } if(frm.passengers.value == "") { alert('Please enter Amount of Passengers'); frm.passengers.focus(); return false; } if(frm.guestname.value == "") { alert('Please enter Name'); frm.guestname.focus(); return false; } if (!validateInt(frm.guestphone.value)) { if(frm.guestphone.value =="" ) alert('Please enter a valid Telephone number'); frm.guestphone.value=""; frm.cguestphone.focus(); return false; } if(!validateEmail(frm.guestemail.value) { alert('Please enter proper email address!'); frm.guestemail.focus(); return false; } function validateInt(val) { switch (isInteger(val)) { case true: return true; break; case false: alert("Please enter numbers only!"); return false; } } function isInteger (s) { var i; if (isEmpty(s)) if (isInteger.arguments.length == 1) return 0; else return (isInteger.arguments[1] == true); for (i = 0; i < s.length; i++) { var c = s.charAt(i); if (!isDigit(c)) return false; } return true; } function isEmpty(s) { return ((s == null) || (s.length == 0)) } function isDigit (c) { return ((c >= "0") && (c <= "9")) } function validateEmail(email){ // This function is used to validate a given e-mail // address for the proper syntax if (email == ""){ return false; } badStuff = ";:/,' \"\\"; for (i=0; i<badStuff.length; i++){ badCheck = badStuff.charAt(i) if (email.indexOf(badCheck,0) != -1){ return false; } } posOfAtSign = email.indexOf("@",1) if (posOfAtSign == -1){ return false; } if (email.indexOf("@",posOfAtSign+1) != -1){ return false; } posOfPeriod = email.indexOf(".", posOfAtSign) if (posOfPeriod == -1){ return false; } if (posOfPeriod+2 > email.length){ return false; } return true } </script> Hello Every1 I need to edit the below form to use JavaScript to Validate if thr Title was put or Not .. its a template i use at my site to add comment and i want the Title to be Requier .Javascript seems the easiest way for me but dont know how. PHP Code: <{if $commentany.com_itemid}> <form action="comment_post.php" method="POST"> <input type="hidden" name="com_itemid" value="<{$commentany.com_itemid}>"> <input type="hidden" name="XOOPS_TOKEN_REQUEST" value="<{$commentany.xoops_token_request}>"> <table class='outer' cellspacing='1' style="margin-top:2em;"> <tr> <th colspan="2"><{$smarty.const._MB_COMMENTANY_LANG_NEW_COMMENT}></th> </tr> <{* <tr> <td class='head' align='left'><{$smarty.const._MB_COMMENTANY_LANG_RULE}></td> <td class='odd' align='left'> <{$commentany.rule_text}> </td> </tr> *}> <tr> <td class='head' align='left'><{$smarty.const._MB_COMMENTANY_LANG_ICON}></td> <td class='odd' align='left'> <{foreach item=i from=$commentany.com_icon}> <input type="radio" name="com_icon" value="<{$i}>"><img src="<{$smarty.const.XOOPS_URL}>/images/subject/<{$i}>"> <{/foreach}> </td> </tr> <tr> <td class='head' align='left'><{$smarty.const._MB_COMMENTANY_LANG_TITLE}></td> <td class='odd' align='left'> <input type="text" name="com_title" size="64" maxlength="255" value="<{$xoops_default_comment_title}>"> </td> </tr> <tr> <td class='head' align='left'><{$smarty.const._MB_COMMENTANY_LANG_TEXT}></td> <td class='odd' align='left'> <{xoopsdhtmltarea name="com_text" cols=50 rows=4}> </td> </tr> <{* <tr> <td class='head' align='left'><{$smarty.const._MB_COMMENTANY_LANG_OPTION}></td> <td class='odd' align='left'> <input type="checkbox" name="dosmiley" value="1" checked><{$smarty.const._CM_DOSMILEY}><br/> <input type="checkbox" name="doxcode" value="1" checked><{$smarty.const._CM_DOXCODE}><br/> <input type="checkbox" name="dobr" value="1" checked><{$smarty.const._CM_DOAUTOWRAP}><br/> </td> </tr> *}> <tr> <td class='head' align='left'></td> <td class='even' align='left'> <input type='submit' class='formButton' name='com_dopreview' id='com_dopreview' value='<{$smarty.const._MB_COMMENTANY_LANG_PREVIEW}>' /> <input type='submit' class='formButton' name='com_dopost' id='com_dopost' value='<{$smarty.const._MB_COMMENTANY_LANG_SUBMIT}>' /> </td> </tr> </table> <input type="hidden" name="dohtml" value="0" /> <input type="hidden" name="dosmiley" value="1" /> <input type="hidden" name="doxcode" value="1" /> <input type="hidden" name="dobr" value="1" /> </form> <{/if}> <{$commentsnav|replace:'button':'hidden'}> Hi guys, just need help so when there is no input into card number, postcode, what are you after, delivery address an error message comes up. Also I need to make it so when someone puts numbers in the first and last name or letters in the postcode it will come up error. I have done this for first and lastname but when I copy my code and try and put it on the others then no javascript works. thanks for your help Code: <html> <head> <script type="text/javascript"> function validate() { var at=document.getElementById("email").value.indexOf("@"); var age=document.getElementById("age").value; var fname=document.getElementById("fname").value; var lname=document.getElementById("lname").value; var ccn=document.getElementById("ccn").value; submitOK="true"; if (fname.length<1) { alert("Please Enter a Valid First Name"); submitOK="false"; } if (isNaN(age)||age<1||age>100) { alert("Age Must be between 1 and 100"); submitOK="false"; } if (lname.length<1) { alert("Please Enter a Last Name"); submitOK="false"; } if (isNaN(age)||age<1||age>100) { alert("Age Must be between 1 and 100"); submitOK="false"; } if (at==-1) { alert("Not a Valid Email Address"); submitOK="false"; } if (submitOK=="false") { return false; if (ccn.length>16) { alert("Credit Card Number is False"); submitOK="false"; } } } </script> </head> <body> <form action="tryjs_submitpage.htm" onsubmit="return validate()"> Name : <input type="text" id="fname" size="20"><br /> Last Name : <input type="text" id="lname" size="20"><br /> Age: <input type="text" size="2" id="age" maxlength="2"> Email: <input type="text" id="email" size="20"><br /> <table><tr> <td><p>Type of Card</p></td> <td><form action="21"> <select name="cars"> <option value="season"> </option> <option value="season">Mastercard</option> <option value="season">Diners Club</option> <option value="season">Visa</option> </select></form></td></tr> </table> <table><tr> <td><p>Expiry Date</p></td> <td><form action="21"> <select name="cars"> <option value="season"> </option> <option value="season">2010</option> <option value="season">2011</option> <option value="season">2012</option> </select></form></td></tr> </table> Card Number : <input type="text" id="ccn" size="20"><br /> Postcode: <input type="text" size="2" id="postcode" maxlength="4"> Delivery Address: <input type="text" id="da" size="20"><br /> Suburb: <input type="text" id="suburb" size="10"><br /> <table><tr> <td><p>What are you after</p></td> <td><form action="21"> <select name="cars"> <option value="poker products"> </option> <option value="poker chips">Poker Table</option> <option value="poker table">Poker Chips</option> <option value="poker table">Both</option> </select></form></td></tr> </table> <input type="submit" value="Submit"> </form> </body> </html> I am new to this forum.. and I am hoping to get help with this javascript validation for my form for my assignment. trying to validate multiple fields. I have never done javascript, so with my limited understanding from Google, I put together code snippets. Problems: 1. How can I get one message box when the user presses submit indicating all failed validation at one time 2. The expired credit card validation doesnt work 3. Cant seem to find a good "confirm password" script 3. The form does not submit if all the fields are correct. <script language="javascript"> <!-- function checkAge() { var bday=parseInt(document.forms[0].txtBday.value); var bmo=(parseInt(document.forms[0].txtBmo.value)-1); var byr=parseInt(document.forms[0].txtByr.value); var byr; var age; var now = new Date(); tday=now.getDate(); tmo=(now.getMonth()); tyr=(now.getFullYear()); { if((tmo > bmo)||(tmo==bmo & tday>=bday)) { age=(tyr-byr) } else { age=(tyr-(byr+1)) } if (age<18) { alert ("you must be over 18 years"); return false; } else { return true; } } } //--> function checkEmail() { var x=document.getElementById("email").value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } { return true; } } function onlyNumbers(evt) { var e = event || evt; // for trans-browser compatibility var charCode = e.which || e.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; { return true; } } function checkCardNumber() { var x = document.getElementById("cardNumber").value; if (x.length<16) { alert("Your Credit Card Number must be 16 digits"); return false; } { return true; } } function checkSecurityCode() { var y = document.getElementById("securityCode").value; if (y.length<3) { alert("Your Security number must be three digits"); return false; } { return true; } } function ValidateExpDate() { var ccExpYear = 20 + $F('<%= expYear.ClientID%>'); var ccExpMonth = $F('<%= expMonth.ClientID%>'); var expDate=new Date(); expDate.setFullYear(ccExpYear, ccExpMonth, 1); var today = new Date(); if (expDate<today) { alert("card expired"); // Credit Card is expire return false; } else { // Credit is valid return true; } } function runall() { checkAge(); checkEmail(); checkCardNumber(); checkSecurityCode(); { return false;} } </script> Hi. I am trying to do some sim[ple form validation using javascript. I have a simple html file Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Send Card</title> <script language="JavaScript" src="gen_validatorv31.js" type="text/javascript"></script> </head> <body> <form action="getName.php" onsubmit="return validate_form(this);"> Receiver (To): <input type="text" name="name"><br> Sender (From): <input type="text" name="sender"><br> Email: <input type="text" name="email"><br> <input type="submit" value="Submit"> </form> </body> </html> Which calls up my javascript and php file (The php file should only be used when the javascript has found no errors). Now I have a js file which does some validation Code: <script type="text/javascript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") { alert(alerttxt); return false; } else { return true; } } } function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) { alert(alerttxt); return false; } else { return true; } } } function validate_form(thisform) { with (thisform) { if (validate_required(name,"Please specify your name!")==false) { name.focus(); return false; } if (validate_required(sender,"Please specify the receivers name!")==false) { sender.focus(); return false; } if (validate_required(email,"Email must be filled out!")==false) { email.focus(); return false; } if (validate_email(email,"Not a valid e-mail address!")==false) { email.focus(); return false; } } } </script> When I run it on my server, I get the form and enter nothing into it, I just click submit. However, it goes straight to the php file and brings up my flash movie, which it shouldnt do. When nothing is entered, the js file should return the error. I thought I had everything set up correctly, but obviously not. Can someone see where I am going wrong? cheers 1.I want to retain the value of the item selected from the drop down list even if the validation fails 2. The input element should be hghlighted red in colour which fails the validation <html> <script type="text/javascript"> <!-- function validate_form1 ( ) { valid = true; if ( document.contact_form1.question1.value=="" || document.contact_form1.question2.value == "" ) { alert ( "Select at least one option." ); //document.getElementById("question1").focus(); document.getElementById('contact_form1').question1.focus(); valid = false; } return valid; } function validate_form2 ( ) { valid = true; if ( document.contact_form2.question1.value=="" || document.contact_form2.question2.value == "" ) { alert ( "Select at least one option." ); //document.getElementById("question1").focus(); valid = false; } return valid; } //--> </script> <table border="5" width="300"> <tr> <td> <form name="contact_form1" action="DBsaving.php" method="post" onSubmit="return validate_form1 ( );"> 1. Question <select name="question1"><option selected="q1" value="">Select One...</option><option value="1">Yes</option><option value="2">No</option></select><br /><br /> 2. Question <select name="question2"><option selected="selected" value="">Select One...</option><option value="1">Yes</option><option value="2">No</option></select><br /><br /> 3. Faculty ID : <textarea name="comments" rows="2" cols="4"></textarea> <input type="submit" value="Submit" /> </form> </td> <td> <form name="contact_form2" action="DBsaving.php" method="post" onSubmit="return validate_form2 ( );" > 1. Question <select name="question1"><option selected="q1" value="">Select One...</option><option value="yes1">Yes</option><option value="no1">No</option></select><br /><br /> 2. Question <select name="question2"><option selected="selected" value="">Select One...</option><option value="yes">Yes</option><option value="no">No</option></select><br /><br /> 3. Faculty ID : <textarea name="comments" rows="2" cols="4"></textarea> <input type="submit" value="Submit" /> </form> </td> </tr> </table> </html> I have a validation issue. I need the input fields to highlight yellow if no text has been entered and when the form is submitted the error message displays to check the highlighted fields. I get the input boxes to highlight when there is no text but on submit the form goes to the next page without the error alert message. Any suggestions would be appreciated. My HTML page: Code: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- Filename: works.htm Supporting files: --> <!-- #BeginEditable "doctitle" --> <title>Wizard Works Order Form</title> <!-- #EndEditable --> <link href="ww.css" rel="stylesheet" type="text/css" /> <script src="functions.js" type="text/javascript"></script> <script src="valid.js" type="text/javascript"></script> </head> <body> <form id="order" action="file:///F:/JavaScript/case4/done.htm" onsubmit="return validateForm()" method="post"> <div id="page"> <div id="head"> <img alt="Wizard Works" src="logo.jpg" /> </div> <ul id="links"> <li><a href="#">Home</a></li> <li><a href="#">Assortments</a></li> <li><a href="#">Firecrackers</a></li> <li><a href="#">Fontains</a></li> <li><a href="#">Cones</a></li> <li><a href="#">Rockets</a></li> <li><a href="#">Sparklers</a></li> <li><a href="#">Online Store</a></li> <li><a href="#">Shopping Cart</a></li> <li><a href="#">Your Account</a></li> <li><a href="#">Safety</a></li> <li><a href="#">Tech Support</a></li> <li><a href="#">Customer Service</a></li> <li><a href="#">About Us</a></li> <li><a href="#">Contact Us</a></li> </ul> <div id="webform"> <fieldset id="purchase"> <legend>Order</legend> <label id="produtLab" class="required" for="product">1) Product</label> <select id="product"> <option value="">Select a Product</option> <option value="3.5">Green/Purple Fountain: $3.50 ea.</option> <option value="4.95">Silver Cone: $4.95 ea.</option> <option value="6.95">Glitter Cone: $6.95 ea.</option> <option value="9.95">Glittering Stars: $9.95 ea.</option> <option value="19.95">Fountain Kit: $19.95 ea.</option> <option value="29.95">Fountain Kit Deluxe: $29.95</option> <option value="39.95">Giant Fountain: $39.95</option> </select> <br /> <label id="qtyLab" class="required" for="qty">2) Quantity</label> <select id="qty"> <option value="">Select a Quantity</option> <option value="1"> 1</option> <option value="2"> 2</option> <option value="3"> 3</option> <option value="4"> 4</option> <option value="5"> 5</option> <option value="6"> 6</option> <option value="7"> 7</option> <option value="8"> 8</option> <option value="9"> 9</option> <option value="10">10</option> </select> <br /> <label id="shippingLab" class="required" for="shipping">2) Shipping</label> <select id="shipping"> <option value="">Select a Shipping Option</option> <option value="4.95">Standard ($4.95)</option> <option value="8.95">Express ($8.95)</option> <option value="12.95">Next Day ($12.95)</option> </select> <br /> <label id="totalLab" for="total">3) Total Calculated Cost</label> <input id="total" readonly="readonly" size="20" type="text" /> </fieldset> <fieldset id="contact"> <legend>Contact Information</legend> <label id="fnameLab" class="required" for="fname">4) First Name</label> <input id="fname" size="20" type="text" class="validate" onblur="changeColor(this)" /> <br /> <label id="lnameLab" class="required" for="lname">5) Last Name</label> <input id="lname" size="20" type="text" onblur="changeColor(this)" /> <br /> <label id="addressLab" class="required" for="address">6) Address</label> <textarea id="address" cols="20" rows="5"onblur="changeColor(this)" ></textarea> <br /> <label id="phoneLab" class="required" for="phone">7) Phone (nnn) nnn - nnnn</label> <input id="phone" size="20" type="text" onblur="changeColor(this)" /> </fieldset> <fieldset id="credit"> <legend>Payment Information</legend> <label for="ccard">8) Credit Card</label> <select id="ccard"> <option value="ae">American Express</option> <option value="dc">Diners Club</option> <option value="disc">Discover</option> <option value="mc">MasterCard</option> <option value="visa">Visa</option> </select> <br /> <label id="cholderLab" class="required" for="cholder">9) Card Holder</label> <input id="cholder" size="20" type="text"onblur="changeColor(this)" /> <br /> <label id="cnumLab" class="required" for="cnum">10) Card Number</label> <input id="cnum" size="20" type="text"onblur="changeColor(this)" /> <br /> <label class="required">11) Expiration Date</label> <select id="cmonth" name="cmonth"> <option>01</option><option>02</option><option>03</option> <option>04</option><option>05</option><option>06</option> <option>07</option><option>08</option><option>09</option> <option>10</option><option>11</option><option>12</option> </select> / <select id="cyear" name="cyear"> <option>2010</option><option>2011</option><option>2012</option> <option>2013</option><option>2014</option><option>2015</option> </select> </fieldset> <input id="sbutton" type="submit" value="Submit Order" onclick="returm validateForm();return false;"/> </div> </div> </form> </body> </html> My valid.js code: Code: /* Functions List: initForm() Initiates the Web form for use by the customer calcCost() Calculates the cost of the customer order validLengths() Validates that empty values have been entered for required fields testLength(field) Tests the length of the text string in the specified field validPatterns() Validates that the field values match their regular expressions testPattern(field, reg) Tests a text string against a specified regular expression validCNum() Tests that the specified credit card number passes the Luhn formula validateForm() Performs a validation test on all of the fields in the form */ window.onload = initForm; var wform; var productIndex = 1; var qtyIndex = 1; var shipIndex = 1; function initForm() { wform = document.forms[0]; wform.product.onchange = calcCost; wform.qty.onchange = calcCost; wform.shipping.onchange = calcCost; wform.onsubmit = validateForm; } function calcCost() { productIndex = wform.product.selectedIndex; productCost = parseFloat(wform.product.options[productIndex].value); qtyIndex = wform.qty.selectedIndex; qtyVal = parseFloat(wform.qty.options[qtyIndex].value); shipIndex = wform.shipping.selectedIndex; shipVal = parseFloat(wform.shipping.options[shipIndex].value); if (productIndex != 0 && qtyIndex != 0 && shipIndex != 0) { wform.total.value = "$"+(productCost*qtyVal+shipVal).toFixed(2); } } function validLengths() { var isValid = true; if (testLength(wform.total)== false) isValid = false; if (testLength(wform.fname)== false) isValid = false; if (testLength(wform.lname)== false) isValid=false; if (testLength(wform.address)== false) isValid=false; if (testLength(wform.phone)== false) isValid=false; if (testLength(wform.cholder)== false) isValid=false; if (testLength(wform.cnum)== false) isValid=false; return isValid; } function testLength(field) { var isValid = true; if (field.value.length == 0) { document.getElementById(field.id+"contact").style.bgcolor="yellow"; isValid = false; } else { document.getElementById(field.id+"contact").style.bgcolor="white"; } return isValid; } function validPatterns() { var isValid = true; phonereg = /^\(?\d{3}[\)-]?\s?\d{3}[\s-]?\d{4}$/; if (testPattern(wform.phone, phonereg) == false) isValid = false; creditType = wform.ccard.selectedIndex; switch (creditType) { case 0: cregx = /^3[47]\d{13}$/; break; case 1: cregx = /^30[0-5]\d{11}$|^3[68]\d{12}$/; break; case 2: cregx = /^6011\d{12}$/; break; case 3: cregx = /^5[1-5]\d{14}$/; break; case 4: cregx = /^4(\d{12}|\d{15})$/; break; } if (testPattern(wform.cnum, cregx) == false) isValid = false; return isValid; } function testPattern(field, reg) { var isValid = true; wsregx = /\s/g; var fv =field.value.replace(wsregx,""); if (reg.test(fv) == false) { isValid = false; document.getElementById(input.id+"fname").style.bgcolor="yellow"; } else { document.getElementById(input.id+"fname").style.bgcolor="white"; } return isValid; } function validCNum() { var isValid = true; wsregx = /\s/g; var fv = wform.cnum.value.replace(wsregx,""); if (luhn(fv)==false) { isValid = false; document.getElementById("cnum").style.bgcolor="yellow"; } else { document.getElementById("cnum").style.bgcolor="white"; } return isValid; } function changeColor(field) { var fv = field.value; if (fv.length==0) { field.style.background="yellow"; } else { field.style.background="white"; } } function validateForm() { var vLengths = validLengths(); var vPatterns = validPatterns(); var vCNum = validCNum(); var vForm = vLengths && vPatterns && vCNum; if (!vForm) { alert("Check the highlighted items in the form for missing/invalid data"); return false; } else { return true; } } |