DaNuGai Posted November 27, 2006 Share Posted November 27, 2006 How do I prevent users from entering a double quote in the INPUT field?Also, is it possible to replace the double quote with single quote? If yes, How?Please help.... Link to comment Share on other sites More sharing options...
aalbetski Posted November 27, 2006 Share Posted November 27, 2006 Here's one way <html> <body> <input type="text" onblur="validatedata(this)" /> </body> <script> function validatedata(object) { var sString = new String(object.value); sString = sString.replace(/"/g,"'"); object.value=sString; } </script></html> Link to comment Share on other sites More sharing options...
midnite Posted November 27, 2006 Share Posted November 27, 2006 hey, here's a better one <input type="text" onKeyPress="return ckeck();">function check(){ return (event.keyCode != 34); // double quot} this is for banning, and the follow one is of replacing <input type="text" onKeyPress="return ckeck();">function check(){ if (event.keyCode == 34){ // double quot var pos = document.selection.createRange(); pos.text = "'"; return false; } else return true;} if you wanna write something likes a forum, or your purpose of doing such restriction or replacement is for protecting data sent to your server or blocking trouble makers for ruinning your layout. i would suggest you to do this in the server side as the JavaScript function can be easily turned off even by an idoit that makes your intrigging protection codes useless. for instant, if you use php, you may try htmlspecialchars() and it will be enough and easy for the purpose Link to comment Share on other sites More sharing options...
DaNuGai Posted November 28, 2006 Author Share Posted November 28, 2006 First of all, I want to thank you for your help....I am actually using JSPs & servlets, so I am not quite sure how I can implement this on the server side. I am validating the data through the servlet but it doesn't catch Double Quotes since it only receives the value of that INPUT field. hey, here's a better one <input type="text" onKeyPress="return ckeck();">function check(){ return (event.keyCode != 34); // double quot} this is for banning, and the follow one is of replacing <input type="text" onKeyPress="return ckeck();">function check(){ if (event.keyCode == 34){ // double quot var pos = document.selection.createRange(); pos.text = "'"; return false; } else return true;} if you wanna write something likes a forum, or your purpose of doing such restriction or replacement is for protecting data sent to your server or blocking trouble makers for ruinning your layout. i would suggest you to do this in the server side as the JavaScript function can be easily turned off even by an idoit that makes your intrigging protection codes useless. for instant, if you use php, you may try htmlspecialchars() and it will be enough and easy for the purpose Link to comment Share on other sites More sharing options...
aspnetguy Posted November 28, 2006 Share Posted November 28, 2006 Well we do not have a Java forum so posting int he General forum will cause less confusion. By posting your question in the JavaScript forum we all assumed you wanted a solution in JavaScript.To do this on server side you will have to capture the input field and use JSP version of the String.Replace function to change (") to (').As far as preventing the user from not even entering a double quote that must be done with Javascript since it is on the client side. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now