最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Best way to forbid quotation marks on input - Stack Overflow

programmeradmin2浏览0评论

Let's assume I have an <input> tag. Value of this tag is used on change. The problem is that if a user inputs single or double quotation marks - it breaks the code. Right now this is solved quite simple:

  1. Override keypress for these keys;
  2. Forbid paste to input.

Here is the sample Code:

HTML

<input id="TestInput"></input>

Javascript

$("#TestInput").keypress(function (e) { // override keypress of " or '
    if (e.which == 13 || e.which == 34 || e.which == 39) {
        return false;
    }
})

.bind("paste", function (e) {// forbid paste
    e.preventDefault();
})

.change(function(){
 var value = $(this).val();
    //and then I use this value for my operations
});

Sample Fiddle

The question: is there a better way to get rid of all quotation marks without forbiding paste?

Note: I assume it can be solved with RegExp, but I'm no good with them, so if you can provide a Regexp - this could work.

Thanks everyone in Advance.

Update-1

On change an ajax call is performed to a method which call the DB and quotes break the query somehow like this: query:

var query = "SELECT Column FROM table WHERE somecolumn LIKE '" + inputVal+ "'%";

if inputVal is something like "foo the resulting sting will look like:

var query = "SELECT Column FROM table WHERE somecolumn LIKE '" + "foo+ "'%";

which obviously breaks the query. + there are no items in the Database which contain quotes.

Let's assume I have an <input> tag. Value of this tag is used on change. The problem is that if a user inputs single or double quotation marks - it breaks the code. Right now this is solved quite simple:

  1. Override keypress for these keys;
  2. Forbid paste to input.

Here is the sample Code:

HTML

<input id="TestInput"></input>

Javascript

$("#TestInput").keypress(function (e) { // override keypress of " or '
    if (e.which == 13 || e.which == 34 || e.which == 39) {
        return false;
    }
})

.bind("paste", function (e) {// forbid paste
    e.preventDefault();
})

.change(function(){
 var value = $(this).val();
    //and then I use this value for my operations
});

Sample Fiddle

The question: is there a better way to get rid of all quotation marks without forbiding paste?

Note: I assume it can be solved with RegExp, but I'm no good with them, so if you can provide a Regexp - this could work.

Thanks everyone in Advance.

Update-1

On change an ajax call is performed to a method which call the DB and quotes break the query somehow like this: query:

var query = "SELECT Column FROM table WHERE somecolumn LIKE '" + inputVal+ "'%";

if inputVal is something like "foo the resulting sting will look like:

var query = "SELECT Column FROM table WHERE somecolumn LIKE '" + "foo+ "'%";

which obviously breaks the query. + there are no items in the Database which contain quotes.

Share Improve this question edited Nov 26, 2013 at 8:14 Max Novich asked Nov 26, 2013 at 7:57 Max NovichMax Novich 1,1699 silver badges20 bronze badges 11
  • 4 Don't prevent the user inputting anything - that's bad for UX. Just make sure your code can handle the quotes. Either escape them, or replace them. – Rory McCrossan Commented Nov 26, 2013 at 7:59
  • 4 More important question is why are quotes breaking your code? – Abhitalks Commented Nov 26, 2013 at 7:59
  • 3 Use SQLParams instead of directly taking unsanitised user input – jasonscript Commented Nov 26, 2013 at 8:15
  • 1 It does have some bearing because depending on what language you've used for your server-side methods, your syntax is going to change. Here's the MSDN article for SQL params in C# – jasonscript Commented Nov 26, 2013 at 8:20
  • 1 You should still be santising user input first. Using SQL Params is a good step in the right direction though – jasonscript Commented Nov 26, 2013 at 8:22
 |  Show 6 more ments

1 Answer 1

Reset to default 6

Only handle the data in the textbox:

$("#TestInput").on('input', function () {
    var value = $(this).val().replace(/'/g, '').replace(/"/g, '');
    // go on with processing data
});
发布评论

评论列表(0)

  1. 暂无评论