JavaScript - Zipcode Validation
Here is my HTML code with Javascript.I'm trying to validate my zip code which should check for empty field and it should be 5 digits.But it's not running.Please check and let me know what's wrong with the code.
Thanks ========================================================= <html> <head> <title>User Interface:The Weather From Your Place</title> <script language="Javascript"> function checkTextField() { var str=/^[\t\s]*$/; value=document.getElementById("zipCode").value; if(value.match(str)) { alert("Please fill your zip code."); return false; } function checkZipCodeValidity() { var zipstr=/^[\d]{5}$/; var zipcode=document.getElementById("zipCode").value; if(zipcode.match(zipstr)) return true; else { alert("Enter a 5 digit zip code."); return false; } } </script> </head> <body> <form name="zipCode"> <div align="center"> <h1>The Weather From Your Place</h1> <p>Please enter the ZIP code</p> <input name="URL" maxlength="10" size="40" > <br > <input type="submit" name="submit" VALUE="Search" onclick="return checkfields();"> </div> </form> </body> </html> Similar TutorialsI have tried many different scripts Regex and different javascripts but nothing seems to work. The code I will send is about the closest I have gotton but it pops up a box whether it is valid or not. I just really know nothing about javascript to even know what to change. Please if someone has an answer or better method I would really appreciate it. Code: <script type="text/javascript"> function validZip(zip) { if (zip.match(/^[0-9]{5}$/)) { return true; } zip=zip.toUpperCase(); if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) { return true; } if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) { return true; } alert('*** Please enter a valid zip code.'); return false; } </script> <input name="ZIP" type="text" id="ZIP" size="6" maxlength="10" onChange="validZip('zip')"/> I have this code which checkes the dutch zipcode, but it's not working properly, the zipcode should be as followed: 1234 AA (with 1 space between the numbers and letters). This is the code I have now: Code: <script language="javascript" type="text/javascript"> function check_nbs_zip(nbs_zip) { var el=findObject(nbs_zip); if(window.RegExp) { var re=RegExp("^[0-9][0-9][0-9][0-9] ?[A-Z][A-Z]$","gi"); if(!re.test(el.value)) { alert('Fout: Postcode GGGG LL'); el.focus(); return false; } } return true; } function checknbs_zip(el) { if(!check_nbs_zip()) { return false; } return true; } </script> Please help. Hey all. I have a simple validation I need to do. I need to just make sure that a Checkbox is checked, and that a Text field has content. Sounds simple but I cannot find any thing that has a check and a text field. Here what I have. Can I modify this script to do this? A Checkbox MUST be checked and Text field MUST be filled out. This currently does the text field fine, but no Checkbox obviously. How can I add a checkbox validation to this? Thats it. Any help is appreciated. Code: <script type="text/javascript"> var textFields = ["digsig"]; function validateForm( ) { var oops = ""; // must initialize this! var form = document.sig; for ( var t = 0; t < textFields.length; ++t ) { var field = form[textFields[t]]; var value = field.value.replace(/^\s+/,"").replace(/\s+$/,""); // trim the input if ( value.length < 1 ) { oops += "You MUST enter your Digital Signature"; } } if ( oops != "" ) { alert("ERROR:" + oops); return false; } } } </script> Hello all, new here Seems like a very nice place to be apart of. I have my website www.gebcn.com. If you view source you will see all that I have done, but more importantly my problem. I have the JS code at the top there and I am unable to W3C validate my HTML because of the JS. I am using XHTML strict and would like to stay using it. The JS I have at the top is my form validation code. I am able to do any validating that I need with this "snippet" of code, I have shrank it from my library version just to use for this newsletter. Until now W3C validating was not important now for some reason it is and I am faced with this problem. I am not a Javascript guy more of a HTML/CSS guy and I can manipulate JS to suit my needs. <problem> I have tried to make this "snippet" of JS code an external file but receive multiple errors with the JS calling for the FORM NAME as it is not on the same page. The form NAME=NEWSLETTER is another problem, as W3C says I am unable to use attribute "NAME" in this location. <problem> I would like to keep the JS close to how it is now as I have a library to use this JS over and over again. Any pointers in the right direction or solutions to my problem would be greatly appreciated. Thanks! Hopefully it is not to hard huh If there is anything anyone needs, the code pasted here, or anything else please let me know. Thanks again! Hello, I dont know if I am going about this the right way, but.. I am trying to validate an email address. If the email is a valid looking email address, it will render the submit button disabled to prevent dupe's while the rest of the script processes. The email validation portion works, but, the part where it renders the form submit button disabled does not: <script type="text/javascript"> 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; submit.disabled = true;} } } function validate_form(thisform) { with (thisform) { if (validate_email(email,"Please enter a valid email address.")==false) {email.focus();return false;} } } </script> Any guidance or help would be apreciated. In my project i have a from which have several fields. The form is <form name="reg" action="payment.jsp" method="post"> ----------- <tr> <td>First name:</td> <td><input type="text" name="firstname"/></td></tr> -------------------------- <input type="submit" value="Next" onclick="fun()" /> ------------ </form> Then to validate the fields i have used JavaScript.They are validating rightly using a alert box. But when i am pressing ok of the alert box the control is passing to the page i have written in action of the form.I want when i will press ok it should come back to the same page.Only if all fields are then it should go to the page refereed in action. Can anyone help me? The JavaScript code is <script type="text/javascript"> function fun() { var x=document.forms["reg"]["firstname"].value; if (x==null || x=="") { alert("First name must be filled out"); } Hi All, I have a java script invoked within an ANT template. This java script as of now converts a text file to xml file. Now, how would I do the schema validation of that XML file against an XSD in the javascript? Saw many examples online but they were using MSXML or java classes Would be glad to have any sort of suggestions.. Thanks Hi, got my form here Code: <form method="POST" onsubmit="return validateFormOnSubmit1(this)" action="sendEmail.php"> <p class="demo2">Receivers E-mail<a class="textFields"> <input type="text" name="receiver" size="30" maxlength="35" /></a></p> <p class="demo2">Senders E-mail<a class="textFields2"> <input type="text" name="sender" size="30" maxlength="35" /></a></p> <p class="btn" > <input name="Submit" type="submit" value="Send"/></p> </form> And that should call up this Code: function validateFormOnSubmit1(theForm) { var reason = ""; reason += validateEmail1(theForm.receiver); reason += validateEmail1(theForm.sender); if (reason != "") { alert("Some fields need correction:\n" + reason); return false; } return true; } function trim(s) { return s.replace(/^\s+|\s+$/, ''); } function validateEmail1(fld) { var error=""; var tfld = trim(fld.value); // value of field with whitespace trimmed off var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ; var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ; if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter an email address.\n"; } else if (!emailFilter.test(tfld)) { //test email for illegal characters fld.style.background = 'Yellow'; error = "Please enter a valid email address.\n"; } else if (fld.value.match(illegalChars)) { fld.style.background = 'Yellow'; error = "The email address contains illegal characters.\n"; } else if (fld.value.length == 0) { fld.style.background = 'Yellow'; error = "The required field has not been filled in.\n" } else { fld.style.background = 'White'; } return error; } For some reason though the validation is not working. Is there anything obvious I am doing wrong? cheers Hey. Could someone point me in the direction of why this is not working, it worked in my last program, but not since I have changed things. I have a standard html form Code: <form method="POST" onsubmit="return validate_form(this)" action="anniversaryPreview.php"> <p class="demo5">Senders Name<a class="textFields2"> <input type="text" name="sender" value="e.g. Love from Nick" size="30" maxlength="35" /></a></p> <p class="btn2" > <input type="submit" value="Preview"></p> </form> And this should call up return validate_form(this) before bringing up the php page. Now the validator is just Code: function validate_required(field, alerttxt) { if (field.value == null || field.value == "") { alert(alerttxt); return false; } else { return true; } } function validate_form(thisform) { if (validate_required(thisform.name,"Please specify the receivers name.")==false) { thisform.name.focus(); return false; } if (validate_required(thisform.sender,"Please specify the sender's name.")==false) { thisform.sender.focus(); return false; } } (This is not in the html file, but its own seperate file) I cant see if I have made any mistakes with the variables names or something, or what I am doing wrong. Dont get any errors, just nothing happens if I leave my forms fields blank. Anyone have an idea of whats going on here? cheers Hello to everyone i need to validate a form Based on radio buttons and text box my code so far Code: <html> <head> <title>My Page</title> <script type="text/javascript"> function validate(thisform) { document.getElementByName("bank");//text box // validate myradiobuttons radio = -1; for (i=thisform.day.length-1; i > -1; i--) { if (thisform.day[i].checked) { radio = i; i = -1; } } if (radio == -1) { alert("You must select all values for the clock !"); return false; } else { return true; } } //bank code validation if (document.getElementByName("bank").value.length>=6) { return true; } else { alert("you must input maximum 6 digit (123456) !"); return false; } } </script> </head> <body> <form action="form-output.php" method="post" id="form"> <input type="radio" name="day" value="f" onclick="dayofweek(this)" /> Sunday (default) <input type="radio" name="day" value="s" onclick="dayofweek(this)" /> Sun </p> <input type="radio" name="month" value="a" onclick="dateformat(this)" /> 28th March 2010 (default) <input type="radio" name="month" value="b" onclick="dateformat(this)" /> 28 March 2010 <input type="radio" name="month" value="c" onclick="dateformat(this)" /> 28th Mar 2010 <input type="radio" name="month" value="d" onclick="dateformat(this)" /> 28 Mar 2010 <input type="radio" name="month" value="e" onclick="dateformat(this)" /> 28/03/2010 <input type="radio" name="month" value="f" onclick="dateformat(this)" /> 28th March 10 <input type="radio" name="month" value="g" onclick="dateformat(this)" /> 28 March 10 <input type="radio" name="month" value="h" onclick="dateformat(this)" /> 28th Mar 10 <br/> <input type="radio" name="month" value="i" onclick="dateformat(this)" /> 28 Mar 10 <input type="radio" name="month" value="j" onclick="dateformat(this)" /> 28/03/10 <input type="radio" name="month" value="k" onclick="dateformat(this)" /> 28th March <input type="radio" name="month" value="l" onclick="dateformat(this)" /> 28 March <input type="radio" name="month" value="m" onclick="dateformat(this)" /> 28th Mar <input type="radio" name="month" value="o" onclick="dateformat(this)" /> 28 Mar <input type="radio" name="month" value="p" onclick="dateformat(this)" /> 28/03 </p> <input type="radio" name="time" value="a" onclick="timeformat(this)" /> 5:28:12 am (Default) <input type="radio" name="time" value="b" onclick="timeformat(this)" /> 5:28 am <input type="radio" name="time" value="c" onclick="timeformat(this)" /> 5:28:12 <input type="radio" name="time" value="d" onclick="timeformat(this)" /> 5:28 <input type="radio" name="time" value="e" onclick="timeformat(this)" /> 17:28:12 <input type="radio" name="time" value="f" onclick="timeformat(this)" /> 17:28 </p> Input a short Bank Code <input type="text" name="bank" size="10" onblur="JavaScript:alert('You must insert a Bank Code')" /></br> <input type="submit" name="submit" onclick="validate(form);return false;" value="Submit" /> <input type="reset" name="reset" value="reset" /> </form> </body> </html> i need on submit to validate if user select values from 3 radio groups and insert the proper data to text box. if not i want to return the proper alerts in text box also i want when lose focus to validate so i put this Code: onblur="JavaScript:alert('You must insert a Bank Code')" it's ok? Thanks in Advance I keep getting validation errors for using && as and. I've read to put "&" in place of it, but whenever I do this, the code stops working. I've tried seperate parenths, and still confused. anyone help to make this work and validate at the same time? if (first=="" && last=="") first = last = "Unknown"; if (first == '') document.writeln(last); else if (last == '') document.writeln(first); else document.writeln (last + ", " + first); Hi am making a form and i used java script for validations .Every thing working fine but i want some thing different from the normal. When some one do not enter the required fields in the form i want a red focus around the form and i don need an alert.Please some one suggest me Am attaching Markup, css and the java script. regards Live Example: Live Example Mark Up : Quote: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <title>Date Of Birth Form</title> <style type="text/css" media="all">@import "form.css";</style> <script type="text/javascript" src="validate_form.js"></script> </head> <body> <div id="page-container"> <form name="applyform" action="#" onsubmit="return validate_form();" method="post"> <label><strong>Date of Birth: </strong>We will use this information to match patient to existing Walgreens records.</label><br /><br /> <label class="labels"><strong>Month: </strong></label><label class="labels"><strong>Day: </strong></label><label class="labels"><strong>Year: </strong></label><br /><br /> <select name="month"> <option value="null"></option> <option value="january">January</option> <option value="february">February</option> </select> <select name="day"> <option value="null"></option> <option value="1">1</option> <option value="2">2</option> </select> <select name="year"> <option value="null"></option> <option value="2011">2011</option> <option value="2010">2010</option> <option value="2009">2009</option> </select><br /><br /> <label><strong>Phone Number: </strong> Example (5551234444)</label><br /><br /> <input type="text" name="phone" class="phonetext"/> <br /><br /> <input type="submit" name="confirm" value="CONFIRM" class="button"/> </form> </body> </html> CSS : Quote: html, body { margin: 0; padding: 0; } #page-container{ width:960px; margin:auto; padding-left:30px; padding-top:40px; } form{ font-family: Arial, Helvetica, Sans-serif; font-size:13px; color:#000; } .labels{ margin-right:60px; } form select{ margin-right:25px; } .phonetext{ width:230px; } .button{ background: url(images/button.png); width:65px; height:20px; font-family:arial, helvetica, sans-serif; font-size:10px; color:#fff; cursorointer; font-weight:bold; border:none; padding-bottom:3px; } Java Script : Quote: function validate_form ( ) { valid = true; if (document.applyform.month.value == "null" ) { alert ( "Select a Month" ); document.applyform.month.focus(); document.applyform.month.style.background = "#dbf4fa"; valid = false; } else if (document.applyform.day.value == "null" ) { alert ( "Select a Day" ); document.applyform.day.focus(); document.applyform.day.style.background = "#dbf4fa"; valid = false; } else if (document.applyform.year.value == "null" ) { alert ( "Select a Year" ); document.applyform.year.focus(); document.applyform.year.style.background = "#dbf4fa"; valid = false; } else if (document.applyform.phone.value == "" ) { alert ( "enter a phone number" ); document.applyform.phone.focus(); document.applyform.phone.style.background = "#dbf4fa"; valid = false; } return valid; } Hey Guys. I am not good at javascript and need a little help. I have a form on a page and it already validates that the user has entered something. But, what I need the validator to do, is ensure that the information submitted is only numbers, and has to be 5 digits. Javascript Function: Code: <SCRIPT LANGUAGE="JavaScript"> function checkFields() { zipcode = document.form.zipcode.value; if (zipcode == "") { alert("Please provide your zip code."); return false; } else return true; } </script> My Form Call: Code: onSubmit="return checkFields();" im suppose to validate an ID text field so that is it mandatory, and must be 6 numeric digits. The code i have for the ID field is: Code: <form id="ThisForm"> <label for="emId"> Employee ID: </label></td> <input type="text" name="empID" id="emId" ;"/> </form> I'm beginner to javascript and really has no idea what to do, can someone help pls. This is a bit of a lazy post but hey, Currently i have an awful lot of checks that run the same check on different variables. Simple Request... Can anyone see an obvious way (which i have clearly missed) to clean up and reduce the amount of code in the below. Code: <? // required code for every page. require($_SERVER['DOCUMENT_ROOT'] . '/scripts/required.php'); // end of required code for every page if(isset($_POST['sbmt'])) { // get variables $new_user = trim($_POST['user']); $new_pass = $_POST['newpass']; $con_pass = $_POST['conpass']; $new_fname = trim($_POST['fname']); $new_email = $_POST['email']; $con_email = $_POST['conemail']; $agree = $_POST['agree']; $level = $_POST['level']; $ip = @$_SERVER['REMOTE_ADDR']; // add slashes $new_user = addslashes($new_user); // encrypt $new_pass_e = hash('ripemd160',$new_pass); $con_pass_e = hash('ripemd160',$con_pass); // check username exists // connect to database $con = mysql_connect(DBHOST, DBUSER, DBPASS); if(!$con) { die('Could not connect: ' . mysql_error());} mysql_select_db(DBNAME, $con); // get row for user $result = mysql_query("SELECT * FROM tUsers WHERE username='$new_user'"); $row = mysql_fetch_array($result); // kill connection mysql_close($con); // check username exists if($row['username']!=null) $user_error = 'Username already in use.<br />'; else $user_error = null; // check email is available // connect to database $con = mysql_connect(DBHOST, DBUSER, DBPASS); if(!$con) { die('Could not connect: ' . mysql_error());} mysql_select_db(DBNAME, $con); // get row for user $result = mysql_query("SELECT * FROM tUsers WHERE email='$new_email'"); $row = mysql_fetch_array($result); // kill connection mysql_close($con); // check email is available if($row['email']!=null) $email_error = 'Email already in use.<br />'; else $email_error = null; // check lengths if(strlen($new_user)<3) $user_len_error = 'Username must be at least 3 characters<br />'; else $user_len_error = null; if(strlen($new_pass)<6) $pass_len_error = 'Password must be at least 6 characters<br />'; else $pass_len_error = null; if(strlen($new_fname)<6) $fname_len_error = 'Your name must be at least 6 characters<br />'; else $fname_len_error = null; // preg matches if(!preg_match('/^[a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $new_email)) $email_preg_error = "Email is not valid.<br />"; else $email_preg_error = null; if(!preg_match('/[a-zA-Z]+\s+[a-zA-Z]+/',$new_fname)) $fname_preg_error = 'Name must containt at least one space.<br />'; else $fname_preg_error = null; // check matches if($new_pass != $con_pass) $pass_match_error = 'The passwords do not match.<br />'; else $pass_match_error = null; if($new_email != $con_email) $email_match_error = 'The emails do not match.<br />'; else $email_match_error = null; // build error list $errorlist = $user_error; $errorlist .= $email_error; $errorlist .= $user_len_error; $errorlist .= $pass_len_error; $errorlist .= $fname_len_error; $errorlist .= $email_preg_error; $errorlist .= $fname_preg_error; $errorlist .= $pass_match_error; $errorlist .= $email_match_error; // set expirey to a month $expire = 0; // set error list cookie setcookie('errorlist',$errorlist,$expire,'/'); // reload register.php header('Location:' . URL . '/register.php'); } ?> The required file is just a series of defines for items like the dbhost, dbusername, dbname, url etc etc. Cheers p.s. I want to add an if statement for if $errorlist=null then do bla else bla... in order for errorlist to be null... do i actually need to define the individual error messages as null. That could get rid of a lot of: else $<error name>=null; I'm pretty sure I can but i'd like to check first. Hello all, I need assistance checking for valid characters in a username/password set up. I have been reading up on form validation but can't find exactly what I am looking for and need help deciphering what's going on. I have a list of accepted characters: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_@" a-z, A-Z, 0-9, dot, dash, underscore, and at. I want to then check an inputed string against that set and if a character is notin the accepted set, to send an alert. Here is what I have gathered so far: a for loop that increments through the values' .length - this is what checks each individual character. Then use of indexOf() can tell me which character is unacceptable. Now is where I am stuck and coming to you. Please help Hi, I have a text-box where you enter the phone nos. On key-press, i have written a validation code to enter only nos and "-". I have disabled Ctrl+v, but my customer want to enable ctrl+v for that text-box only. My requirement is after pasting the value it should automatically check for validation.. How to do it? Thanks... I have this php validation string
Code: $urlregex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$"; and i want it to convert into javascript. please help Let's start by saying that I stink at javascript. I've taken multiple different tutorials, some more than once, and I still can't get it. So, when I need a code, I search for it, and try different ones until I find something that works. I've been looking for a code for email validation for quite a long time, and I still can't find something that works with my code for validating the other parts of my contact form. I can get it to check for the presence of text, but not for the presence of an @ sign and period. I would like for someone to please help me figure this out. Right now I have the following code: Javascript Code: <!-- function validate_form ( ) { valid = true; if ( document.contact.firstnamecontact.value == "" ) { alert ( "Please fill in the 'First Name' box." ); valid = false; } if ( document.contact.lastnamecontact.value == "" ) { alert ( "Please fill in the 'Last Name' box." ); valid = false; } if ( document.contact.emailcontact.value == "" ) { alert ( "Please fill in a valid email address." ); valid = false; } if ( document.contact.subjectcontact.value == "" ) { alert ("Please fill in the 'Subject' box." ); valid = false; } if ( ( document.contact.topic[0].checked == false ) && ( document.contact.topic[1].checked == false ) && ( document.contact.topic[2].checked == false ) ) { alert ( "Please choose your Topic of Inquiry" ); valid = false; } if ( document.contact.messagecontact.value == "" ) { alert ( "Please specify a question/comment." ); valid = false; } return valid; } //--> HTML Code: <form name="contact" method="post" action="contactcompletion.php3" onsubmit="return validate_form ( );"> <table> <tr> <td><b>First Name: </td><td><input type="text" name="firstnamecontact"/></td> </tr> <tr> <td><b>Last Name: </b></td><td><input type="text" name="lastnamecontact"/></td> </tr> <tr> <td><b>Email: </b></td><td><input type="text" name="emailcontact"/></td> </tr> <tr> <td><b>Subject: </b></td><td><input type="text" name="subjectcontact"/></td> </tr> </table> <br /> <br /> <h4>Topic of Inquiry (Choose One):</h4> <table> <tr> <td><input type="radio" name="topic" value="questionscontact" /><b> Questions</b></td> <td><input type="radio" name="topic" value="commentscontact" /><b> Comments</b></td> <td><input type="radio" name="topic" value="othercontact" /><b> Other</b></td> </tr> </table> <br /> <br /> <h4>Enter Your Questions or Comments:</h4> <br /> <textarea name="messagecontact" rows="10" cols="70"></textarea> <br /> <br /> <p><input type="submit" name="send" value="Send" /></p> Thanks a bunch in advance for helping me!! |