) for this regular expression: [^'"]+ This is obviously ...">

HTML5 Sample Input for Quote

I need to check the input field ( <input type="text" /> ) for this regular expression:

 [^'"]+ 

This is obviously not an easy task since I cannot use " inside an attribute. Using the quot entity does not work, because it is interpreted literally using the regex mechanism.

How can i do this?

+4
source share
3 answers

The HTML5 @pattern attribute regular expression is based on a JavaScript regular expression, so you can use the hexadecimal notation or Unicode notation for this char, as in JavaScript:

 " = \x22 OR \u0022 ' = \x27 OR \u0027 

So you can easily use

 pattern="[^'\x22]+" 

TADA! No JavaScript libraries !;)

+22
source

Try using a js framework like jQuery or PrototypeJS

then you can say

 onchange=(function() {...}); 

you can follow how to use regex in jQuery through this thread: Validating an expression in jQuery

-one
source

If you have an HTML5 browser, you can write

 <input id="ipt_my_field" type="text" name="my_field_name" pattern="[^'"]+" 

and you need to prepare for broken browsers Modernizer with some similar

 if(!Modernizr.input.pattern) { var input = document.getElementById("ipt_my_field"); input.addEvent('onblur', function(ip){ if(ip.value.match(/[^'"]+/)) { // is a good } else { // is a bad } }); } 
-one
source

Source: https://habr.com/ru/post/1397210/


All Articles