JavaScript - Postcode Validator - Need To Clear A Field.
Hi, basically it's a simple postcode validator.
It checks to see whether the postcode is in UK format, then tells you if it isn't with an alert box, then I want it to clear the field. The input field's ID is postcode. Code: function testPostCode () { var myPostCode = document.getElementById('postcode').value; if (checkPostCode (myPostCode)) { document.getElementById('postcode').value = checkPostCode (myPostCode) } else {alert ("Please enter your Postcode correctly, this includes:\n \n * Correct Spacing - AA1 1AA \n * Correct amount of letters and numbers. \n \n Sorry if this causes any inconvenience, but it is to your benefit.")}; document.postcode.value=""; } Anyone able to help me? Similar TutorialsI have a simple script connect to a radio button. Based on which option, the matching div appears with a text box - and one with a radio button. If they change their mind and click another radio button, it'll switch to the new div, but keep the content in the old. How do I clear that out? I looked for a snippet, but they're all for clearing default text in a field when you click it. I want something a little different. Here's what I'm using: Code: <script> $(document).ready(function() { $("div.desc").hide(); $("input[name$='hear']").click(function() { var test = $(this).val(); $("div.desc").hide(); $("#" + test).show(); }); }); </script> <input type="radio" name="hear" value="dcranch"> DC Ranch Community <br /> <input type="radio" name="hear" value="friend"> From a friend <br /> <div style="display: none;" id="dcranch" class="desc"> <input type="checkbox" value="Yes" name="resident" /> If so, are you a DC Ranch resident? )</div><br /> <div style="display: none;" id="friend" class="desc"> Name? <input type="text" name="how1" /></div><br /> Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="javascript/text"> <!-- function clearInputField( input, def ) input = document.getElementById( input ); if ( input.value == def ) { input.value = ''; } } --> </script> </head> <body> <b>Testing Input</b> <input id="input" onclick="JavaScript: clearInputField('input', 'Enter Text');" type="text" size="31" value="Enter Text" /> <body> </html> This gives me a "Uncaught ReferenceError: clearInputField is not defined" error and I'm not sure why...? i was wondering what event would clear an input field that has a value in it... so a user can type their own value.... any examples? Hello, i Have problem about clear value when ppl input data on text field Any idea? Code: <form action="http://www.packagetrackr.com/track/" method="get" name="track" target="_blank"> <div class="form-field-error-message hidden" style="display: none; "> <div class="CommonEmptyTrackingNumber"></div> </div> <div id="track-form-tracking-number-container"> <div id="track-form-tracking-number-inner-container" class="track-form-control-boxer"><input id="track-form-tracking-number" class="track-form-element " type="text" name="n" value="Type a tracking number" title="Type a tracking number..." onFocus="if(/^Type a tracking number...$/i.test(this.value)){this.value='';}" onBlur="if(this.value==''){this.value='Type a tracking number...';}"> <input value="Track!" name="submit" type="submit" title="Track your package" class="track-form-submit-button sprite-master"> </div> </div> <div id="track-form-jss-carrier-container" class="hidden" style="z-index: 1000; display: none; "> <div id="track-form-jss-carrier-content"> <label id="jss-carrier-auto" class="track-form-jss-carrier-label jss-carrier-label"> <input id="jss-carrier-auto-value" value="" type="radio" name="c" checked="checked"> <span>Auto Detect Carrier</span> </label> </div> </div> </form> I need some help as this is driving me crazy. I have a onblur, focus with this code for the columns of footer filter. Code: $("tfoot input").keyup( function () { /* Filter on the column (the index) of this element */ oTable.fnFilter( this.value, $("tfoot input").index(this) ); } ); $("tfoot input").each( function (i) { asInitVals[i] = this.value; } ); $("tfoot input").focus( function () { if ( this.className == "search_init" ) { this.className = ""; this.value = ""; } } ); $("tfoot input").blur( function (i) { if ( this.value == "" ) { this.className = "search_init"; this.value = asInitVals[$("tfoot input").index(this)]; } } ); I am clearing the input fields with this. Code: $("input:button").click(function(e) { $('#col1').val(''); $('#col2').val(''); $('#col3').val(''); $('#col4').val(''); $('#col5').val(''); Including a reset of the filters, but won't bore with that, it works, the filters for the columns are all released and reset. My issue is the tfoot fields are cleared just fine, but remain blank. The onblur values do not refill the fields until I click in, then click out of the input field. Do I need to adjust for the clearing of the input field in the original focus and onblur? Or do I need a better reset of the the .val('') statement with a tweak to let the onblur function to show. Thanks, any help really appreciated. Tom I was wondering if there was a simple way of notifying a user who has entered an incorrect postcode? I have found numerous answers to this problem but i am sure there is a simpler way to do it as ones i have found have rows and rows of code. Help is much appreciated Hi all, I have a script that validates a postcode, including checking that the 4th character from the end of the postcode is a space. To save sending you back to the postcode textbox to correct the error, how can I automatically force a space at the 4th character position from the end of the postcode? ie, CM12JB is wrong. Need to change it to CM1 2JB. Any ideas? Gary I have a search form and added the "Clear All" Button functionality...but it clears the last part of the search form display preferences....how do I only clear the checkboxes at the top of the form only and not at the bottom of the form between the <DIV> tags? I have attached a copy of the JSP page. Hi, I would like to set the postcode Code: <?php echo $pubdetails['rsPostCode]; ?> as a default value in the form field so that the map area loads up automatically for instance the link above uses postcode bn11 3rg thanks http://auspost.com.au/apps/postcode.html Basically I want to build the same thing. The data is freely available there as a download. I couldn't find anything existing in the jquery plugin space. Can anyone offer pointers on how to achieve it? Hello! I have started using php/html/javascript just recently so I'm not too familiar with it yet, however I think I've done alright. I have apache installed so I can mess around with my files and perfect stuff, so I've created a form that a user fills out, pushes a button, and it prints the information in an organized fashion. It works perfectly for the most part, and I have a validator script that ensures all of the fields work correctly. All of that works great, however I recently uploaded all of the files to a hosted server and the validator stopped picking up for some reason. Anyone have any idea why on localhost a form validator script would work but hosted it wouldn't? Any insight would be great. I've added the two scripts below: Form (php) code:http://pastebin.com/J83AX6GG Validator (JS) code: http://pastebin.com/LMuLu3Jp Thank you for your time! Hello! I'm doing an assignment that validates a password field, the password field should only contain numbers, however this code only verifies the first number input in the field, which means i need to check through every singel input in the field, so i got a little problem <script> function checkPassword(strng) { var text = strng.value; var regel = text.replace(/[\(\)\.\-\ ]/g, ''); if(isNaN(parseInt(text))) { window.alert("Fel, password is not a number:" + text); } } else { window.alert("correct e-mail and password!"); } } </script> I've been thinking about a for loop should fix the problem, but im new to javascript, some help would be appreciated! Hello, I don't see what the problem is with my form validation script. It does not validate. Please point it out to me. Code: <script type="text/javascript"> function validateForm() { var x=document.forms["myForm"]["name"].value; if (x==null || x=="") { alert("First name must be filled out"); return false; } var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Please enter a valid email address"); return false; } var x=document.forms["myForm"]["message"].value; if (x==null || x=="") { alert("You have not entered a message"); return false; } } </script> html Code: <form name="myForm" action="contact.php" onsubmit="return validateForm()" method="post"> First name: </p> <input type="text" name="name"> </p> Email address: </p> <input type="text" name="email"> </p> Subject: </p> <input type="text" name="subject"> </p> Message: </p> <textarea name="message" rows="15" cols="50"></textarea> </p> <input type="submit" value="Submit"> </form> the script is in the <head> section and html in the <body>. Thank you. hiiii, im new to javascripts . Can anyone help me to know that a javascript which is validating a phone number accepts only digits but if the text field is left empty it should accept the entry as an empty entry.... plz share knowledge u will be more knowledgeable <script type="text/javascript"> Code: function switch1(div) { if (document.getElementById('one')) { var option=['one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve']; for(var i=0; i<option.length; i++) { obj=document.getElementById(option[i]); obj.style.display=(option[i]==div)? "block" : "none"; }}}// function switchImg(i){document.images["wine"].src = i;} </script> used in this page seems to object to option.length Frank. Because of my rather second-rate vision; I would like to get an editor for my Javascript that will check my syntax for me. Missing or extra brackets, semicolons etc are the bane of my life. Any suggestions welcomed. Frank Hi There, I really need your help. I am aware that there is no isDate() function for javascript, however, how could one check a date against a validator to ensure that the date entered is either returned true or false the date format that I would like to check would consist of the dd/mm/yyyy so essentially: isDate("12/01/2012") would return as true isDate("N/A") = would return as false I have searched high and low and am counting on the experts on this forum for some major help. A huge thanks in advance and I really appreciate everything. Cheers, Jay I have the following script in the head; Code: <script type="text/javascript"> <![CDATA[ <!--This function validates the data from the three input boxes on the MAILTO form--> function validate() { Check="Valid"; Name=document.contact.name.value; Mail=document.contact.mail.value; Comment=document.contact.comment.value; if (Name=="your name" || Name.length<1) { alert("Please enter your name"); Check="Invalid"; } else if (Mail.indexOf("@")<1 || Mail.indexOf(".")<1) { alert("Please enter a valid email address"); Check="Invalid"; } else if (Comment=="your comment" || Comment.length<1) { alert("Please enter your comment"); Check="Invalid"; } if (Check=="Invalid") { return false; } else { alert("Thank you for your comment"); } } ]]> </script> and the following form in the body; Code: <div> <form name="contact" onsubmit="return validate()" action="email.asp" method="post"> <div class="input_title">Name:</div><input type="text" name="name" size="25" class="input_box" /> <br /> <div class="input_title">Your Email:</div><input type="text" name="sender" size="25" class="input_box" /> <br /> <div class="input_title">Phone Number:</div><input type="text" name="tel" size="25" class="input_box" />(Optional) <br /> Message: <br /> <textarea name="enquiry" cols="50" rows="6" class="input_box"></textarea> <br /> <input type="submit" value="Submit" name="submit" /><input type="reset" value="Reset" name="reset" /> <br /> </form> <br /><br /> </div> It is a aspx file and validates fine as XHTML Trans. Why won't it work? Hello, I am trying to add a simple script to my CSS menu for a little bit more eye candy. However, when I add the script - it causes issues in my online store (ex; the javascript-powered one page checkout stops working). Here is the exact error from the error console: Quote: Error: jQuery.validator is undefined Source File: https://www.gem-tech.com/store/pc/onepagecheckout.asp Line: 288 And here is the exact script: Quote: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript" src="jquery.hoverIntent.minified.js"></script> <script type="text/javascript"> $(document).ready(function() { function megaHoverOver(){ $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Calculate width of all ul's (function($) { jQuery.fn.calcSubWidth = function() { rowWidth = 0; //Calculate row $(this).find("ul").each(function() { rowWidth += $(this).width(); }); }; })(jQuery); if ( $(this).find(".row").length > 0 ) { //If row exists... var biggestRow = 0; //Calculate each row $(this).find(".row").each(function() { $(this).calcSubWidth(); //Find biggest row if(rowWidth > biggestRow) { biggestRow = rowWidth; } }); //Set width $(this).find(".sub").css({'width' :biggestRow}); $(this).find(".row:last").css({'margin':'0'}); } else { //If row does not exist... $(this).calcSubWidth(); //Set Width $(this).find(".sub").css({'width' : rowWidth}); } } function megaHoverOut(){ $(this).find(".sub").stop().fadeTo('fast', 0, function() { $(this).hide(); }); } var config = { sensitivity: 2, // number = sensitivity threshold (must be 1 or higher) interval: 100, // number = milliseconds for onMouseOver polling interval over: megaHoverOver, // function = onMouseOver callback (REQUIRED) timeout: 500, // number = milliseconds delay before onMouseOut out: megaHoverOut // function = onMouseOut callback (REQUIRED) }; $("ul#topnav li .sub").css({'opacity':'0'}); $("ul#topnav li").hoverIntent(config); }); </script> I believe there is some sort of conflict? Not sure though. Any input on this? Thanks, |