Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript RegEx partial match
    primarykey
    data
    text
    <p>I have a regular expression pattern, which validates for a three digit number</p> <pre><code>/^\d{3}$/.test("123") // true /^\d{3}$/.test("123.") // false </code></pre> <p>I want to use this regex as an input restriction on a textbox.</p> <p>Basically, if the new value matches, i allow the character to by typed, otherwise i prevent it.</p> <p>The problem is that no value will ever match, becase "1" is not a full match, and will not allow me to type it.</p> <p>Is it any way of testing a partial match for a regEx in javascript?</p> <pre><code>/^\d{3}$/.test("123") // true /^\d{3}$/.test("12") // "partial match" /^\d{3}$/.test("a12") // false </code></pre> <p><strong>EDIT</strong></p> <p>\d{3} was just an example. I need to use an email regex or a phone regex as input restriction.</p> <pre><code>"email" // true "email@" // true "email@@" // false "@yahoo.com" // false </code></pre> <p><strong>EDIT 2</strong></p> <p>I have a textBox plugin with input restriction based on a regular expression.</p> <p>The regular expression can be anything, a hex color Regex, for example: (#){1}([a-fA-F0-9]){6}</p> <p>I need to prevent user to insert characters which doesn't match the regex.</p> <p>For example, if the textbox is empty, the first allowed character would be "#".</p> <p>But if i test "#" character against the regex, it will return "false", because "#" by itself is not valid.</p> <pre><code>/^(#){1}([a-fA-F0-9]){6}$/.test("#") // false </code></pre> <p>But at the same time, "#" is partial valid because it respects the regex format (and i should allow user to type it)</p> <p>What i need to know is if i can verify if a string is a partial match of a regex, so i can allow the user to type the character.</p> <pre><code>/^(#){1}([a-fA-F0-9]){6}$/.test("#") // is a partial match, allow type /^(#){1}([a-fA-F0-9]){6}$/.test("#0") // is a partial match, allow type /^(#){1}([a-fA-F0-9]){6}$/.test("#00") // is a partial match, allow type /^(#){1}([a-fA-F0-9]){6}$/.test("#000") // is a partial match, allow type /^(#){1}([a-fA-F0-9]){6}$/.test("#0000") // is a partial match, allow type /^(#){1}([a-fA-F0-9]){6}$/.test("#00000") // is a partial match, allow type /^(#){1}([a-fA-F0-9]){6}$/.test("#000000") // is a partial match, allow type /^(#){1}([a-fA-F0-9]){6}$/.test("#000000D") // is not a match, prevent typing </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload