simora Posted January 6, 2013 Share Posted January 6, 2013 I'm trying to pass a form field value to substring() to evaluate it. my form field is like this; Word here : <input type="text" name="word" value = " " onmouseout="functionones()"> I need to make sure that "word" is numeric only and that it begins with the number 1 If NOT, I want to show an alert and send them to another location. I tried this, but its not working. <script type="text/javascript"> function functionones() var f =(document.form1.word.value): var x = f.substring(0,1); { if (var x >1) alert("Your word data is wrong"); window.location = 'http://www.basy.biz'; } //--> </script> If I hard code a value of "123343466" into var f and do a doc.write, I'll get 1 Appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/ Share on other sites More sharing options...
codefossa Posted January 6, 2013 Share Posted January 6, 2013 Your variables are declared outside of the function for a start. Why not just set the max length of the input field to 1? JS is client sided, so it's the same security .. If you need JS to deal with it on top of that, I would still keep the input's length limit just to make things easier for the client. Maybe not even bother reading the value from the field .. Demo: http://xaotique.no-ip.org/tmp/40/ HTML <form method="post" action="#"> <input type="text" maxlength="1" id="word" style="width: 15px; text-align: center;" /> <span id="result"></span> </form> Javascript // Wait for window to load window.addEventListener("load", function() { // Wait for keyup to trigger window.document.querySelector("#word").addEventListener("keyup", function(event) { // Check inputted value var input = event.which >= 65 && event.which <= 90 ? String.fromCharCode(event.keyCode) : null; // Check if key pressed is a-z (add more above) if (input != null) { // Set the input field to the character. this.value = input; } else { // Key pressed wasn't a-z } }, false); }, false); Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1403757 Share on other sites More sharing options...
simora Posted January 7, 2013 Author Share Posted January 7, 2013 Xaotique: THANKS ! I got this to work like this: <script language="javascript"> function showAlert() { var f =document.form1.word.value; var x = f.substring(0,1); if(x > 1){ alert(x +" is greater than 1 "); window.location = "http://www.basy.com/"; } } </script> Now I'm Having a problem evaluating these 2 conditions if(x > 1) || (!IsNumeric(x)) I need x to be 1 and Numeric. If there's a letter in the first position, this will catch it. RE: Why not just set the max length of the input field to 1 .The field is used for multiple things. The # 1 at the beginning is my way to filter specific people. Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1403789 Share on other sites More sharing options...
haku Posted January 7, 2013 Share Posted January 7, 2013 (edited) Now I'm Having a problem evaluating these 2 conditionsif(x > 1) || (!IsNumeric(x)) You're over thinking this. Just check if it is 1: if(x != 1) Though I'd probably do a more strict check myself: if(x !== "1") Edited January 7, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1403809 Share on other sites More sharing options...
simora Posted January 7, 2013 Author Share Posted January 7, 2013 My mistake. I want to evaluate if f IsNumeric NOT x i.e If X ==1 & f IsNumeric Sorry about that. Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1403978 Share on other sites More sharing options...
haku Posted January 8, 2013 Share Posted January 8, 2013 A freebee for you. This will test if the first digit is one, and the rest of the characters after the one are also numeric. If the first digit is not 1, or any of the rest of the characters are not numeric, then it will return false. if(f.match(/^1\d+$/)) Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1404086 Share on other sites More sharing options...
simora Posted January 8, 2013 Author Share Posted January 8, 2013 Thanks haku: Also found this after you pointed me in the Javascript match direction: http://www.devarticles.com/c/a/Javascript/Regular-expressions-in-Javascript/6/ Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1404113 Share on other sites More sharing options...
haku Posted January 8, 2013 Share Posted January 8, 2013 (edited) Once again this editor screws things up. Here's the link for anyone else who wants it: http://www.devarticles.com/c/a/Javascript/Regular-exp<b></b>ressions-in-Javascript/6/ Edit: Can't even fix it. Google it if you want the link. Edited January 8, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1404116 Share on other sites More sharing options...
codefossa Posted January 8, 2013 Share Posted January 8, 2013 (edited) Here's a working link. Yeah .. I cheated. Edited January 8, 2013 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/272775-pass-a-form-field-value-to-javascript-substring/#findComment-1404118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.