toolman Posted September 10, 2012 Share Posted September 10, 2012 Hi, I am trying to validate a field so the user must enter some text and also only be allowed to enter alphanumeric characters, but it doesn't work. Any ideas what I have wrong? I have this code: function validateForm() { var x=document.forms["Form"]["inputtext"].value; if (x==null || x=="") { alert("You must enter some text!"); return false; } if (/[^0-9a-bA-B\s]/gi.test(inputtext.value)) { alert ("Only alphanumeric characters and spaces are valid in this field"); fieldname.value = ""; fieldname.focus(); return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/ Share on other sites More sharing options...
lemmin Posted September 11, 2012 Share Posted September 11, 2012 a-b should be a-z You probably don't need A-Z since you have the case-insensitive modifier. Or maybe you don't need the case-insensitive modifier. That took a surprising amount of staring to catch! Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1377037 Share on other sites More sharing options...
toolman Posted September 11, 2012 Author Share Posted September 11, 2012 Thanks. I now have this, but it still doesn't work function validateForm() { var x=document.forms["Form"]["inputtext"].value; if (x==null || x=="") { alert("You must enter some text!"); return false; } if (/[^0-9a-zA-Z\s]/gi.Form(inputtext.value)) { alert ("Only alphanumeric characters and spaces are valid in this field"); inputtext.value = ""; inputtext.focus(); return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1377053 Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 What doesn't work? How? What do you see when you use js debugging tools like Chrome or Firebug. Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1377055 Share on other sites More sharing options...
lemmin Posted September 11, 2012 Share Posted September 11, 2012 You changed the regex object function from test() to form(), which doesn't exist. It should be like this: if (/[^0-9a-zA-Z\s]/gi.test(inputtext.value)) Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1377059 Share on other sites More sharing options...
toolman Posted September 11, 2012 Author Share Posted September 11, 2012 I've changed the form() to test(), but it doesn't seem to validate. The validation works if there is no text entered, but if I enter any other characters apart from letters, it submits the form. Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1377061 Share on other sites More sharing options...
haku Posted September 12, 2012 Share Posted September 12, 2012 [^0-9a-zA-Z\s] The carat (^) you entered at the start means that it should match any characters OTHER than the ones listed. Remove the carat. Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1377159 Share on other sites More sharing options...
lemmin Posted September 12, 2012 Share Posted September 12, 2012 The carat (^) you entered at the start means that it should match any characters OTHER than the ones listed. Remove the carat. I think that was his intention; disapprove the input if it contains any character OTHER than what is in the regex. Where are you calling this function? You have to call it onsubmit or sooner. Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1377219 Share on other sites More sharing options...
haku Posted September 16, 2012 Share Posted September 16, 2012 You're right, I wasn't looking at the original code, only the regex. It should be something like this: if (someValue.match(/[^0-9a-zA-Z\s]/gi)) { alert ("Only alphanumeric characters and spaces are valid in this field"); } Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1378267 Share on other sites More sharing options...
Adam Posted September 16, 2012 Share Posted September 16, 2012 You also can't make use of the modifiers with the shorthand notation. You need to manually create a new RegExp object and pass them in as the second argument, then use that within your condition. Edit Actually, I'm completely wrong here! Ignore that, I always thought it was the case. Quote Link to comment https://forums.phpfreaks.com/topic/268235-validation-issue/#findComment-1378280 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.