XpertWorlock Posted January 17, 2009 Share Posted January 17, 2009 I'm trying to kill two birds with one stone, when you type in the input, if you type an illegal character, it deletes it on you, and if you click the button, it runs the script, and only follows through if it comes back false. It works perfectly, doing what it's supposed to do, but I wanted to allow it to have spaces, the only problem is, that, if you click the button, it allows the illegal characters and doesn't stop. <html> <head> <script type="text/javascript"> //NEED TO BE ABLE TO ALLOW ONE SPACE function illegalCharacters(id) { var illegalChars = /\W/; if (illegalChars.test(id.value)) { id.value = (id.value.slice(0, -1)) return true; } else { return false; } } function updateDiv(id) { if (illegalCharacters(id) == true) { return; } document.getElementById('div').innerHTML = id.value } </script> </head> <body> <input id="testBox" type"text" onkeyup="illegalCharacters(testBox)"> <input type="button" value="Click" onclick="updateDiv(testBox)"> <div id="div" style="border:1px solid black;width:200px; padding:20px;"></div> </body> </html> If you try /\W / instead, you will see what I mean. Also I have another question, since this is a hacked version of someone else's illegal character script, how come it has to be if (illegalChars.test(id.value)) and not if (illegalChars(id.value)) ?? Thanks Mike Quote Link to comment https://forums.phpfreaks.com/topic/141245-solved-space-problem-validation/ Share on other sites More sharing options...
btherl Posted January 19, 2009 Share Posted January 19, 2009 The regexp for \W or space is /[\W ]/ The regexp for \W followed by space is /\W / About the syntax, illegalChars is an object. You can call an object's methods, but you cannot call an object itself. "test" is a method of illegalChars. As an analogy, you could say btherl.eat(burger), but not btherl(burger), otherwise I wouldn't know what to do with the burger. Quote Link to comment https://forums.phpfreaks.com/topic/141245-solved-space-problem-validation/#findComment-740149 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.