JavaScript - Validating For Blankspaces In The Begining And End Of A Textbox
How can I check whether the user has entered only blankspaces in a textbox or the user has entered blankspaces at the end of a value inserted into txtbox?
In my form I've validated the textboxes only for empty values?? The code goes likee, Code: if(document.getElementById('fname').value=="") { document.frm1.fname.style.border='1px solid red'; //document.frm1.fname.style.background='#FFFFCC'; document.frm1.fname.focus(); document.getElementById('d_fname').style.visibility="visible"; document.getElementById('d_fname').innerHTML="Enter First Name"; } else { document.frm1.fname.style.border='1px solid #4F6AD7'; document.frm1.fname.style.background='#FFF'; document.getElementById('d_fname').style.visibility="hidden"; f++; } What extra additions should I make inorder to validate the textbox for blank spaces? Similar TutorialsHi. Please help in validating both a drop down list and textbox if the other is not empty alert message will pop-up and vice versa. The validation will be such as follows: 1. If each drop down list is selected and the textbox next to it is not equal to 0 or not empty, DO NOTHING. 2. If drop down list is not selected and textbox is empty DO NOTHING. Below condition will check if both drop down list and textbox if one of the other is empty then message will appear: 3. If drop down list is selected or index is not equal to 0 AND textbox is empty or equal to 0, alert will pop-up. 4. If drop down list is not selected or index is equal to 0 AND textbox is not empty or not equal to 0, alert will pop-up. I guess with the condition above will validate each drop down list and textboxes show to each other. Below is my code that I use and it doesn't work. Please help making it work. Of course I didn't include the <script tag> but it is oncluded in the original page. Thanks. Code: function validation_OK(iRows,iCols) { var iRows; var iCols; var desigamt = new Array(iRows); desigamt[0] = Number(document.getElementById('i').value); desigamt[1] = Number(document.getElementById('j').value); desigamt[2] = Number(document.getElementById('k').value); desigamt[3] = Number(document.getElementById('l').value); desigamt[4] = Number(document.getElementById('m').value); desigamt[5] = Number(document.getElementById('n').value); desigamt[6] = Number(document.getElementById('o').value); desigamt[7] = Number(document.getElementById('p').value); desigamt[8] = Number(document.getElementById('q').value); desigamt[9] = Number(document.getElementById('r').value); var i; var j; for (i=0; i < iRows; i++) { desigamt[i] = new Array(iCols); iCols[0] = document.getElementById('I').value; iCols[1] = document.getElementById('J').value; iCols[2] = document.getElementById('K').value; iCols[3] = document.getElementById('L').value; iCols[4] = document.getElementById('M').value; iCols[5] = document.getElementById('N').value; iCols[6] = document.getElementById('O').value; iCols[7] = document.getElementById('P').value; iCols[8] = document.getElementById('Q').value; iCols[9] = document.getElementById('R').value;; for (j=0; j < iCols; j++) { if(a[i][j] = "" || a[i][j] != "") { return true; } else { if (a[i].selectedIndex!=0 && a[j].value == "") { var answer = confirm("Please enter the amount to designate on the selected partner agency. Clicking the CANCEL button will delete the partner agency selected. Do you want to continue?") if (answer) { a[j].focus(); } else { a[i].selectedIndex=0; } } else { var answer = confirm('Please select a partner agency to designate the amount you entered. Clicking the CANCEL button will delete the amount you entered. Do you want to continue?') if (answer) { a[i].focus(); } else { a[j].value=0; } } } } } return (desigamt); } Below is my html code. FYI: I have lots of function included and this function I am trying to make it work is one of them. Please help!!!!! Code: <table> <tr> <td> <select id="I" name="I" size="1" style="overflow-x:scroll; width:380px; " onchange="return validation_OK();"> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['I']); ?> value="<?php echo $prompt; ?>"><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="i" name="i" class="text" type="text" value="<?php safeEcho($form['i'])?>" style="width:90px;" onChange="CalculateTotal();return validation_OK();" /> <?php helper_error('i');?> </td> </tr> <tr> <td> <select id="J" name="J" size="1" style="overflow-x:scroll; width:380px; " onchange="return validation_J();"> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['J']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="j" name="j" class="text" type="text" value="<?php safeEcho($form['j'])?>" style="width:90px;" onChange="CalculateTotal();return validation_J();" /> <?php helper_error('j');?> </td> </tr> <tr> <td> <select id="K" name="K" size="1" style="overflow-x:scroll; width:380px; " > <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['K']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="k" name="k" class="text" type="text" value="<?php safeEcho($form['k'])?>" style="width:90px;" onChange="CalculateTotal()" /> <?php helper_error('k');?> </td> </tr> <tr> <td> <select id="L" name="L" size="1" style="overflow-x:scroll; width:380px; " > <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['L']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="l" name="l" class="text" type="text" value="<?php safeEcho ($form['l'])?>" style="width:90px;" onChange="CalculateTotal()" /> <?php helper_error('l');?> </td> </tr> <tr> <td> <select id="M" name="M" size="1" style="overflow-x:scroll; width:380px; " > <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['M']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="m" name="m" class="text" type="text" value="<?php safeEcho($form['m'])?>" style="width:90px;" onChange="CalculateTotal();" /> <?php helper_error('m');?> </td> </tr> <tr> <td> <select id="N" name="N" size="1" style="overflow-x:scroll; width:380px; " > <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['N']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="n" name="n" class="text" type="text" value="<?php safeEcho($form['n'])?>" style="width:90px;" onChange="CalculateTotal();" /> <?php helper_error('n');?> </td> </tr> <tr> <td> <select id="O" name="P" size="1" style="overflow-x:scroll; width:380px; " > <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['O']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="o" name="o" class="text" type="text" value="<?php safeEcho($form['o'])?>" style="width:90px;" onChange="CalculateTotal();" /> <?php helper_error('o');?> </td> </tr> <tr> <td> <select id="P" name="P" size="1" style="overflow-x:scroll; width:380px; " > <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['P']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="p" name="p" class="text" type="text" value="<?php safeEcho($form['p'])?>" style="width:90px;" onChange="CalculateTotal();"/> <?php helper_error('p');?> </td> </tr> <tr> <td> <select id="Q" name="Q" size="1" style="overflow-x:scroll; width:380px; " > <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['Q']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="q" name="q" class="text" type="text" value="<?php safeEcho($form['q'])?>" style="width:90px;" onChange="CalculateTotal();" /> <?php helper_error('q');?> </td> </tr> <tr> <td> <select id="R" name="R" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['R']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="r" name="r" class="text" type="text" value="<?php safeEcho($form['r'])?>" style="width:90px;" onChange="CalculateTotal();" /> <?php helper_error('r');?> </td> </tr> </table> I hope you can help me. Thanks a lot. Hi.. I have form and i want to input data using barcode and it will display on textbox and after input on the first textbox the focus will go to next textbox untill it will go on the last textbox and on the last textbox it will automatically save the data's on the database. How is it possible? here is my sample code: Code: <?php error_reporting(0); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <html> <head> <script type="text/javascript"> function ini() { // Retrieve the code var code =document.getElementById ("code_read_box1").value; var code =document.getElementById ("code_read_box2").value; var code =document.getElementById ("code_read_box3").value; var code =document.getElementById ("code_read_box4").value; var code =document.getElementById ("code_read_box5").value; var code =document.getElementById ("code_read_box6").value; // Return false to prevent the form to submit return false; } </script> </head> <body onLoad="document.barcode.code_read_box1.focus()"> <form name=barcode onsubmit = "return ini()"> <input type="text" id="code_read_box1" value="" /><br/> <input type="text" id="code_read_box2" value="" /><br/> <input type="text" id="code_read_box3" value="" /><br/> <input type="text" id="code_read_box4" value="" /><br/> <input type="text" id="code_read_box5" value="" /><br/> <input type="text" id="code_read_box6" value="" /><br/> </form> </body> </html> Hello. I've been teaching myself HTML and CSS for a while, and now I've moved into the world of Javascript (but I'm still very much a beginner). For practice, I've been building a sample sign up form for a mock website, and I'm having problems with the birthdate section. The idea I had was to have MM, DD, and YYYY be the default values of my 3 textboxes (as an example for users), then set the value to nothing when the box gained focus. That all works fine, but I ran into problems when I tried to write an if statement to set the value back to MM, DD, or YYYY if the value was still nothing when the user clicked away. As it is now it just replaces the text inputted into the textbox (which of course is not good). Any ideas for what the problem might be? Code: <form name="signup" action="javascript:void(0);" method="post"> <table> <tr> <td>Date Of Birth:</td> <td> <input name="DOBmonth" type="text" value="MM" size="2" style="color: #555555;" onFocus="clearDOBmonth()" onBlur="restoreDOBmonth()" /> <input name="DOBday" type="text" value="DD" size="2" style="color: #555555;" onFocus="clearDOBday()" /> <input name="DOByear" type="text" value="YYYY" size="4" style="color: #555555;" onFocus="clearDOByear()" /></td> </tr> <tr> <td></td> <td><button name="Submit" type="submit" style="font-size: 1em;">Sign Up</button></td> </tr> </table> </form> <script type="text/javascript" language="javascript"> <!-- document.signup.DOBmonth.value="MM"; document.signup.DOBday.value="DD"; document.signup.DOByear.value="YYYY"; function clearDOBmonth() { document.signup.DOBmonth.value="";} function clearDOBday() { document.signup.DOBday.value="";} function clearDOByear() { document.signup.DOByear.value="";} function restoreDOBmonth() { if ('document.signup.DOBmonth.value = ""') { document.signup.DOBmonth.value = "MM"; // alert(document.signup.DOBmonth.value); } } //--> </script> Another side question, if I set a variable equal to the value of a textbox, then change the value of the variable, does that also change the value of the textbox? or is the variable no longer associated with the textbox. Example: Code: var a = document.form.textbox.value; a = blue; does document.form.textbox.value = blue? or is var a now completely independent of the textbox value? Thanks! Hi im not good with javavscript so got some code off the net to do some rollovers, however it doesnt validate. can anyone please help me as to why this maybe? 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>Untitled Document</title> <link rel="stylesheet" type="text/css" href="joho.css" /> <SCRIPT TYPE="text/javascript"> <!-- // copyright 1999 Idocs, Inc. http://www.idocs.com/tags/ // Distribute this script freely, but please keep this // notice with the code. var rollOverArr=new Array(); function setrollover(OverImgSrc,pageImageName) { if (! document.images)return; if (pageImageName == null) pageImageName = document.images[document.images.length-1].name; rollOverArr[pageImageName]=new Object; rollOverArr[pageImageName].overImg = new Image; rollOverArr[pageImageName].overImg.src=OverImgSrc; } function rollover(pageImageName) { if (! document.images)return; if (! rollOverArr[pageImageName])return; if (! rollOverArr[pageImageName].outImg) { rollOverArr[pageImageName].outImg = new Image; rollOverArr[pageImageName].outImg.src = document.images[pageImageName].src; } document.images[pageImageName].src=rollOverArr[pageImageName].overImg.src; } function rollout(pageImageName) { if (! document.images)return; if (! rollOverArr[pageImageName])return; document.images[pageImageName].src=rollOverArr[pageImageName].outImg.src; } //--> </head> <body> <div id="wrapper"> <div id="contentwrapper"> <div id="col1"> <ul> <li><A HREF="home.html" onMouseOver = "rollover('home')" onMouseOut = "rollout('home')" ><img src="menu/row1sq1_home_OP.png" border="0" alt="Home" NAME="home"/></a><SCRIPT TYPE="text/javascript"> <!-- setrollover("rollovers/row1sq1_home_RO.png"); //--> </SCRIPT></li> <li><img src="menu/row2sq1_OP.png" alt="2"/></li> <li><A HREF="illustrations.html" onMouseOver = "rollover('illustrations')" onMouseOut = "rollout('illustrations')" ><img src="menu/row3sq1_illustrations_OP.png" border="0" alt="illustrations" NAME="illustrations"/></a><SCRIPT TYPE="text/javascript"> <!-- setrollover("rollovers/row3sq1_illustrations_RO.png"); //--> </SCRIPT></li> <li><img src="menu/row4sq1_OP.png" alt="4"/></li> </ul> </div> <div id="col2"> <ul> <li><img src="menu/row1sq2_OP.png" alt="1"/></li> <li><img src="menu/row2sq2_OP.png" alt="2"/></li> <li><img src="menu/row3sq2_OP.png" alt="3"/></li> <li><A HREF="portraits.html" onMouseOver = "rollover('portraits')" onMouseOut = "rollout('portraits')" ><img src="menu/row4sq2_portraits_OP.png" border="0" alt="portraits" NAME="portraits"/></a><SCRIPT TYPE="text/javascript"> <!-- setrollover("rollovers/row4sq2_portraits_RO.png"); //--> </SCRIPT></li> </ul> </div> <div id="col3"> <ul> <li><A HREF="about.html" onMouseOver = "rollover('about')" onMouseOut = "rollout('about')" ><img src="menu/row1sq3_about_OP.png" border="0" alt="About" NAME="about"/></a><SCRIPT TYPE="text/javascript"> <!-- setrollover("rollovers/row1sq3_about_RO.png"); //--> </SCRIPT></li> <li><A HREF="cards.html" onMouseOver = "rollover('cards')" onMouseOut = "rollout('cards')" ><img src="menu/row2sq3_cards_OP.png" border="0" alt="cards" NAME="cards"/></a><SCRIPT TYPE="text/javascript"> <!-- setrollover("rollovers/row2sq3_cards_RO.png"); //--> </SCRIPT></li> <li><img src="menu/row3sq3_squeak_OP.png" border="0" alt="1"/></li> <li><A HREF="contact.html" onMouseOver = "rollover('contact')" onMouseOut = "rollout('contact')" ><img src="menu/row4sq3_contact_OP.png" border="0" alt="contact" NAME="contact"/></a><SCRIPT TYPE="text/javascript"> <!-- setrollover("rollovers/row4sq3_contact_RO.png"); //--> </SCRIPT></li> </ul> </div> <div id="col4"> <ul> <li><img src="menu/row1sq4_OP.png" alt="1"/></li> <li><img src="menu/row2sq4_OP.png" alt="2"/></li> <li><A HREF="weddings.html" onMouseOver = "rollover('weddings')" onMouseOut = "rollout('weddings')" ><img src="menu/row3sq4_wedding_OP.png" border="0" alt="weddings" NAME="weddings"/></a><SCRIPT TYPE="text/javascript"> <!-- setrollover("rollovers/row3sq4_wedding_RO.png"); //--> </SCRIPT></li> <li><img src="menu/row4sq4_OP.png" alt="4"/></li> </ul> </div> </div> </div> </body> </html> thank Q C Code: <img class="border" alt="googtatic_map" src="http://maps.google.com/maps/api/staticmap?center=51.454863,0.011673&zoom=13&markers=United+Reformed+Church,+111+Burnt+Ash+Road,++Lee,+London+SE12+8RG,+UK&size=250x250&sensor=true" /> I have the above code and it is not validating with the w3c validation for XHTML it has thrown up 8 errors and 14 warnings. Should I ignore them or what? As I do like my code to validate. Hi everyone, Would any of you know a cross-platform way to validate XML against XSD in JavaScript? All examples found online use MSXML, which is only available under Windows/IE. Using remote web-services is not a option, since the script should be able to run online. Cheers, Vit Hello all, I am pretty new to javascript and could use some help. I am validating a page (image attached) to make sure that total hours entered do not exceed the allocated amount, which is stored in the database. I have already validated to make sure that they enter a number and that it is not greater than 8. Could some one help me with the rest? thanks so much, Joe Code: var returncode = true var inputs = document.getElementsByTagName("INPUT") for(var x=0; x<inputs.length; x++) { if(inputs[x].type =='text' && isNaN(inputs[x].value)) { document.getElementById("hourserror").style.display = "block"; inputs[x].style.backgroundColor = '#FFCCCC'; returncode = false; } if(inputs[x].value > 8) { document.getElementById("totalerror").style.display = "block"; inputs[x].style.backgroundColor = '#FFCCCC'; returncode = false; } } return returncode; hi i need a help in java script. i have some text fields and when i enter the information in it and enter the validation button i need the results to be displayed in a box below these btns. Is it possible in Java script? Please see the attached image for some ideas. cheers I managed to create the form that asks you to type in the information but I'm having some difficulty trying to figure out the alert to say "Thank you " after you have everything filled in and then hitting the button. my code is Code: <HTML> <HEAD> <TITLE> Form Validation Example </TITLE> <SCRIPT LANGUAGE="JavaScript"> function validatePersonalInfo(){ var _first = document.info.fname.value; var _city = document.info.city.value; var _phone = document.info.phone.value; if(_first.toString() == ""){alert("Please enter a first name.");} if(_city.toString() == ""){alert("Please enter your city.");} if(_phone.toString() == ""){alert("Please enter your phone number.");} var phoneInput = document.info.phone.value; var validPhone = false; var validCity = false; if(checkCity == true){ validCity = true; } else{ if(!checkPhone(phoneInput)){ alert("Phone number is invalid." + validPhone); } else{ validPhone = true; } if(validCity && validPhone){ alert("Your form has been verified"); } } } function checkPhone(str){ var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4})$/; return regexp.test(str); } function checkNum(length){ var cityLet = parseInt(cityEntry, 10); if (document.info.city.value.length == length){ if(cityLet != 0 && isNaN(cityLet) == false){ return true; } else { return false; } } else { return false; } } </script> </head> <body> <p> <form name="info" action="" method="post"> <table> <tr><td align="left">First Name:</td> <td align="left"> <input type="text" name="fname" size=15> </td> </tr> <br> </tr> <br> <tr> <td align="left">City:</td> <td align="left"> <input type="text" name="city" size=15> </td> </tr> <br> <tr><td align="left">phone</td> <td align="left"> <input type="text" name="phone" size=20></td> </tr> <br> </tr> <br> </table> <center> <input type="button" value="Submit" onClick="validatePersonalInfo()"> </center> </form> </body> </html> the help is greatly appreciated Hi Guys, I use this code to validate e-mail addresses: PHP Code: // checks if the e-mail address is valid var emailPat = /^(".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/; var matchArray = formSignup.txtEmail.value.match(emailPat); if (matchArray == null) { alert("Your email address seems incorrect. Please try again (check the '@' and '.'s in the e-mail address)"); return false; } What i noticed today, is if a customer registers with an e-mail like: something.something@hotmail.com the first dot throws up the error, i'm not to great on regex lol any help would be appreciated thanks guys Graham Hi Folks I am trying to write a js that validates a form with two text inputs. The two inputs a 'D_techA' and 'D_techB'. I just want to add up (sum) the two input fields (which must be positive numbers) and make sure that they add up to exactly 100 (not more and not less). If they do not add up to 100, then an alert should pop up that says "The two values must add up to exactly 100." I have tried and tried to write a js that does this validation (looking at numerous validation scripts posted in this forum and elsewhere) and I just cannot seem to make it work. I would very much appreciate any suggestions! Thanks! The html code for my very simple form is: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="form1" name="form1" method="post" action=""> <label>Investment in Tech A <input name="D_techA" type="text" id="D_techA" /> </label> <label>Investment in Tech B <input name="D_techB" type="text" id="D_techB" /> </label> <p> <input name="Submit" type="submit"/> </p> </form> </body> </html> Hi guys! I have tried numerous attempts in getting the Address field to work (i.e. validated). I want it to accept letters, numbers, spaces and commas! However, it won't let me! Below is the code I have provided. Code: function validateAddress(fld) { var error = ""; var illegalChars = /[\W_]/; // } else if (illegalChars.test(fld.value)) { fld.style.background = 'Yellow'; error = "Your Billing address contains illegal characters!\n"; } else { fld.style.background = 'White'; } return error; } Thanks a lot! I have two arrays that are tied together Ink1Data[][InkID] and Ink1Data[][Ink1Desc] I also have this Javascript code to validate it: Code: var chksink1 = document.getElementsByName("Ink1Data[][InkID]"); for (var i = 0; i < chksink1.length; i++) { switch (chksink1[i]) { case (chksink1.value=1); var i1Desc = "Ink1Data["+i+"][Ink1Desc]"; if (validate_required(i1Desc,"Please fill in a trim size.")==false) {i1Desc.focus();return false;} break; case (chksink1.value=2); var i1Desc = "Ink1Data["+i+"][Ink1Desc]"; if (validate_required(i1Desc,"Please fill in folding instructions.")==false) {i1Desc.focus();return false;} break; case (chksink1.value=3); var i1Desc = "Ink1Data["+i+"][Ink1Desc]"; if (validate_required(i1Desc,"Please fill in scoring instructions.")==false) {i1Desc.focus();return false;} break; } } basically, I want to count how many are in the array (will be the same number for both) and then depending on the value of the first array, validate the second and kick an appropriate error if necessary. it should be pretty easy, but I can't seem to get the code to work. Is there anyone that could please help me figure this out? Thank you! Figured it out. Thanks
I want to validate two a date range using javascript, so that my program would check startdate less than end date. The below function only check the year, and it works. Code: function validate() { var start=document.entry.datum1.value; year = start.substr(0, 4); month = start.substr(5, 2); dayval = start.substr(8, 2); var end=document.entry.datum2.value; year1 = end.substr(0, 4); month1 = end.substr(5, 2); dayval1 = end.substr(8, 2); var myDate=new Date(); myDate.setFullYear(year,month,dayval); var today = new Date(); today.setFullYear(year1,month1,dayval1); if (myDate> today) { alert("The start date must come before the end date"); return false } return true; } But I have write a function for checking the whole date value and it didnt work. My date is in format- 2008-10-23(yyyy-mm-dd) Code: function validate() { var start=entry.datum1.value; year = start.substr(0, 4); month = start.substr(5, 2); dayval = start.substr(8, 2); intyear = parseInt(year, 10); intmonth = parseInt(month, 10); intdayval = parseInt(dayval, 10); var end=entry.datum2.value; year1 = end.substr(0, 4); month1 = end.substr(5, 2); dayval1 = end.substr(8, 2); intyear1 = parseInt(year1, 10); intmonth1 = parseInt(month1, 10); intdayval1 = parseInt(dayval1, 10); var startDate = Date.parse(intyear,intdayval,intmonth); var endDate = Date.parse(intyear1,intdayval1,intmonth1); if (startDate > endDate) { alert("The start date must come before the end date"); return false; } return true; } Please help Hello, So all I need to do is have a checkbox above the 'submit' button that the user must check before the button becomes active. Once the user checks the checkbox, then the button becomes active - and users can click on this button to get to the new page. However, I haven't found any tutorials online that handle this exactly. What I have found is example scripts that have multiple checkboxes, etc...and if I try to remove any of them from the script, it seems to mess up the functionality. So - take this for example. This would actually work perfect, if the male/female option wasn't there, and the 'reset' wasn't there either: Quote: <head> <script type="text/javascript"> function validate(form) { // Checking if at least one period button is selected. Or not. if (!document.form1.sex[0].checked && !document.form1.sex[1].checked){ alert("Please Select Sex"); return false;} if(!document.form1.agree.checked){alert("Please check the terms and conditions"); return false; } return true; } </script> </head> <body> <table border='0' width='50%' cellspacing='0' cellpadding='0' ><form name=form1 method=post action=action_page.php onsubmit='return validate(this)'><input type=hidden name=todo value=post> <tr bgcolor='#ffffff'><td align=center ><font face='verdana' size='2'><b>Sex</b><input type=radio name=sex value='male'>Male </font><input type=radio name=sex value='female'><font face='verdana' size='2'>Female</font></td></tr> <tr><td align=center bgcolor='#f1f1f1'><font face='verdana' size='2'><input type=checkbox name=agree value='yes'>I agree to terms and conditions </td></tr> <tr bgcolor='#ffffff'><td align=center ><input type=submit value=Submit> <input type=reset value=Reset></td></tr> </table> </body> I try to manually remove the 'male/female' options for this in Dreamweaver, but then the script doesn't work properly. Can anybody help me out here? Would be greatly appreciated. Thank you Just wondering what the code would be to validate the radio buttons, thus when i click get score a pop up will tell me that one of the radio buttons was not selected. If you really want to help me it would be good if i was able to have get score and submit working on one button, and validating every field. My code is below (don't mind the questions and answers ) Code: <HEAD> <script language="JavaScript"> var numQues = 5; var numChoi = 3; var answers = new Array(5); answers[0] = "Life"; answers[1] = "Disturbing"; answers[2] = "Yahoo Answers"; answers[3] = "Playing Video Games in the Basement"; answers[4] = "About the portion of my Sandwich"; function getScore(form) { var score = 0; var currElt; var currSelection; for (i=0; i<numQues; i++) { currElt = i*numChoi; for (j=0; j<numChoi; j++) { currSelection = form.elements[currElt + j]; if (currSelection.checked) { if (currSelection.value == answers[i]) { score++; break; } } } } score = Math.round(score/numQues*100); form.percentage.value = score + "%"; var correctAnswers = ""; for (i=1; i<=numQues; i++) { correctAnswers += i + ". " + answers[i-1] + "\r\n"; } form.solutions.value = correctAnswers; } function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") { alert(alerttxt);return false; } else { return true; } } } function echeck(str) { var at="@" var dot="." var lat=str.indexOf(at) var lstr=str.length var ldot=str.indexOf(dot) if (str.indexOf(at)==-1){ alert("Please Enter a Valid E-mail") return false } if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ alert("Please Enter a Valid E-mail") return false } if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ alert("Please Enter a Valid E-mail") return false } if (str.indexOf(at,(lat+1))!=-1){ alert("Please Enter a Valid E-mail") return false } if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ alert("Please Enter a Valid E-mail") return false } if (str.indexOf(dot,(lat+2))==-1){ alert("Please Enter a Valid E-mail") return false } if (str.indexOf(" ")!=-1){ alert("Please Enter a Valid E-mail") return false } return true } function validate_form(thisform) { with (thisform) { if (validate_required(Name,"Name must be filled out!")==false) {Name.focus();return false;} if (validate_required(Surname,"Surname must be filled out!")==false) {Surname.focus();return false;} { var emailID=document.submitting.email if ((emailID.value==null)||(emailID.value=="")){ alert("Please Enter a Valid E-mail") emailID.focus() return false } if (echeck(emailID.value)==false){ emailID.value="" emailID.focus() return false } return true } } } </script> </HEAD> <BODY> <h3>Quiz</h3> <form name="quiz"> 1. What do i win at..? <ul style="margin-top: 1pt"> <li><input type="radio" name="q1" value="Life">Life</li> <li><input type="radio" name="q1" value="Nothing At All">Nothing At All</li> <li><input type="radio" name="q1" value="Using a Computer">Using a Computer</li> </ul> 2. My Life is..? <ul style="margin-top: 1pt"> <li><input type="radio" name="q2" value="Boring">Boring</li> <li><input type="radio" name="q2" value="Disturbing">Disturbing</li> <li><input type="radio" name="q2" value="Like everyone elses">Like everyone elses</li> </ul> 3. I don't learn off my mother i learn off ..? <ul style="margin-top: 1pt"> <li><input type="radio" name="q3" value="Google">Google</li> <li><input type="radio" name="q3" value="Yahoo Answers">Yahoo Answers</li> <li><input type="radio" name="q3" value="The Bible">The Bible</li> </ul> 4. I Spend Most of my time ..? <ul style="margin-top: 1pt"> <li><input type="radio" name="q4" value="Cutting Down Palm Tree's">Cutting Down Palm Tree's</li> <li><input type="radio" name="q4" value="Eating">Eating</li> <li><input type="radio" name="q4" value="Playing Video Games in the Basement">Playing Video Games in the Basement</li> </ul> 5. I Complain the most about ..? <ul style="margin-top: 1pt"> <li><input type="radio" name="q5" value="JavaScript">JavaScript</li> <li><input type="radio" name="q5" value="About the portion of my Sandwich">About the portion of my Sandwich</li> <li><input type="radio" name="q5" value="Lag">Lag</li> </ul> <input type="button" value="Get score" onClick="getScore(this.form)"> <input type="reset" value="Clear answers"> <p> Score = <strong><input class="bgclr" type="text" size="5" name="percentage" disabled></strong><br><br> Correct answers:<br> <textarea class="bgclr" name="solutions" wrap="virtual" rows="4" cols="30" disabled> </textarea> </form> <form action="submit.htm" onsubmit="return validate_form(this)" method="post" name="submitting"> Name: <input type="text" name="Name" size="30"> <br> <br> Surname: <input type="text" name="Surname" size="30"><br><br> Email: <input type="text" name="email" size="30"><br> <form action="submit.htm" onSubmit="return validate_form(this)" method="post"> <input type="submit" value="Submit" > </form> </body> <HTML> <HEAD> <TITLE>Form Validation Example </TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function validateForm() { if(document.UD.FIRST_NAME.value=="") { alert("PLEASE FILL IN THE First Name FIELD"); return(false); }// validation for empty first name if(document.UD.LAST_NAME.value=="") { alert("PLEASE FILL IN THE Last Name FIELD"); return(false); }// validation for empty last name if(document.UD.MONTH.value=="") { alert("PLEASE FILL IN THE Birth Date FIELD"); return(false); }//validation for empty month if(document.UD.DAY.value=="") { alert("PLEASE FILL IN THE Birth Date FIELD"); return(false); } //validation for empty day if(document.UD.YEAR.value=="") { alert("PLEASE FILL IN THE Birth Date FIELD"); return(false); }//validation for empty year if(document.UD.MONTH.value >=1 && document.UD.MONTH.value <=12) { return(true); } else{ alert("Please enter a valid Month"); return(false); } /* this runs its course but stops after it makes this statement true. It does not continue the last to if statements. I have rearranged the code to read: if(document.UD.MONTH.value <=0) { alert("Please enter a valid Month"); return(false); } if(document.UD.MONTH.value >=12) { alert("Please anter a valid Month") return(false); } but by following these statments it skips these and goes straight to the next if statement and follows that until it reaches true then it won't go onto the last if statment.*/ if(document.UD.DAY.value >=1 && document.UD.DAY.value <=32) { return(true); } else{ alert("Please enter a valid day"); return(false); } if(document.UD.YEAR.value >=1900 && document.UD.YEAR.value <=2011) { return (true); } else { alert("Please enter a valid year"); return(false); } return(true); } //--> </SCRIPT> </HEAD> <BODY> <CENTER> <FONT SIZE="5" COLOR="#006600" FACE="verdana">Form Validation</FONT> </CENTER><BR><BR> <FORM METHOD=POST ACTION="http://www.mysite.com" NAME="UD"> <TABLE ALIGN="CENTER"> <TR> <TD>First Name</TD> <TD><INPUT TYPE="TEXT" NAME="FIRST_NAME"></TD> </TR> <TR> <TD>Last Name</TD> <TD><INPUT TYPE="TEXT" NAME="LAST_NAME"></TD> </TR> <TR> <TD>Birth Date</TD> <TD><INPUT TYPE="TEXT" SIZE="4" NAME="MONTH"><INPUT TYPE="TEXT" SIZE="4" NAME="DAY"><INPUT TYPE="TEXT" SIZE="6" NAME="YEAR"> </TD> </TR> <TR> <TD><INPUT TYPE="SUBMIT" VALUE="Submit Form" onClick="return validateForm()"></TD> <TD><INPUT TYPE="RESET" VALUE="Reset"></TD> </TR> </TABLE> </FORM> </BODY> </HTML> Above is my code. I have included a brief snippet of my problem in the code already, but basically my code runs through all the if statements until it gets to the if statement about the month and validating what numbers are used. Once it completes that if statement it returns true and goes no further. However if I rearrange my code it still does not work. If anyone has some idea on what I am doing I would greatly appreciate it. I am new to javascript and have been working on this for hours upon hours trying to figure it out so any help would be greatly appreciated. Thank you. |