15

I have a regular expression pattern, which validates for a three digit number

/^\d{3}$/.test("123")   // true
/^\d{3}$/.test("123.")  // false

I want to use this regex as an input restriction on a textbox.

Basically, if the new value matches, i allow the character to by typed, otherwise i prevent it.

The problem is that no value will ever match, becase "1" is not a full match, and will not allow me to type it.

Is it any way of testing a partial match for a regEx in javascript?

/^\d{3}$/.test("123")   // true
/^\d{3}$/.test("12")    // "partial match"
/^\d{3}$/.test("a12")   // false

EDIT

\d{3} was just an example. I need to use an email regex or a phone regex as input restriction.

"email"        // true
"email@"       // true
"email@@"      // false
"@yahoo.com"   // false

EDIT 2

I have a textBox plugin with input restriction based on a regular expression.

The regular expression can be anything, a hex color Regex, for example: (#){1}([a-fA-F0-9]){6}

I need to prevent user to insert characters which doesn't match the regex.

For example, if the textbox is empty, the first allowed character would be "#".

But if i test "#" character against the regex, it will return "false", because "#" by itself is not valid.

/^(#){1}([a-fA-F0-9]){6}$/.test("#") // false

But at the same time, "#" is partial valid because it respects the regex format (and i should allow user to type it)

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.

/^(#){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
Catalin
  • 11,503
  • 19
  • 74
  • 147

6 Answers6

2

You could partially validate the email address by using ()? for more letters and/or characters. Every ()? going deeper in the validation tree.

The following regular expression pattern validates email address letter by letter.

^[a-zA-Z]+(@{1}[a-zA-Z]*(\.{1}[a-zA-Z]*)?)?$

It does not take into account every possibility out there, but for basic ones like aa@bb.dd it works just fine and there's room to improve it further.

Ilu
  • 894
  • 6
  • 12
1

You would be better off by using a library like maskedinput.js. You can then setup your text input like follows:

jQuery(function($){
    $("#your_input").mask("999");
});

UPDATE

you can use a validator for forms and preset specific types of fields to validate

epoch
  • 16,396
  • 4
  • 43
  • 71
  • jquery masked input plugin is used for fixed sized strings. I need something generic. – Catalin Jan 30 '12 at 08:21
  • Yes, but that is something different. I need to "validate" on keypress not on blur. – Catalin Jan 30 '12 at 08:29
  • @RaraituL, it shouldn't be hard to modify the script to cater for that need, or even search for something similiar, there are loads of form/input validators on the net :) – epoch Jan 30 '12 at 08:48
  • i've been doing the research, and is nothing like this :( I don't need a plugin, i need a solution, if exists. – Catalin Jan 30 '12 at 09:20
0

You'll want to use explicit "|" partial matches. For your color matching example it's pretty simple, you just need to explicitly match an empty string partial

/^(|#[a-f0-9]{0,6})$/i.test(inputStr)

For an email it's more complicated since there are more partial match combinations

/^(|\w+|\w+@|\w+@\w+|\w+@\w+\.|\w+@\w+\.\w+)$/.test(inputStr)

Note that you can't get away with something like /^(|\w*@|...)$/ since that matches @blah.com which isn't a valid partial input.

mjhm
  • 16,497
  • 10
  • 44
  • 55
  • I have a similar scenario, do you know what I need to partial match a string like "ABC12345" where the first part is a series of letters and the second part is numeric? – Shawn T Mar 19 '20 at 23:31
0

You can specify a range in the expression so that it matches anything between one and three digits like so:

/^\d{1,3}$/.test("1")  // true
/^\d{1,3}$/.test("12")  // true
/^\d{1,3}$/.test("123a")  // false
Michael Sandino
  • 1,818
  • 1
  • 13
  • 10
  • The regex which i used just as an example. I will normally use a email regex or a phone regex, and i should allow user to type "email@" but not allow him to type "email@@" for example. – Catalin Jan 30 '12 at 08:15
0

Just provide a regex that allows for partial matches. e.g. /^\d{1,3}$/

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
0

According to your last edit, this should work:

/^#[a-fA-F0-9]{0,6}$/
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Yes, but this is still hard coded. I need a "is partial match" response from a regex test. Doesn't matter what regex is used. Something like this [RegExPlus](http://codesaway.info/RegExPlus/partial.html) which is for Java. – Catalin Jan 30 '12 at 11:15
  • It validates, but is still hard coded. I should be able to have any regular expression not just the email case. For example, i might want to have /^[a-Z]{5}@?$/.test("email") // true, and /^[a-Z]{5}@?$/.test("email2") // false – Catalin Jan 30 '12 at 13:21
  • @RaraituL: Sorry, I don't understand what you mean exactly. Could you edit your question and provide some real test cases? – Toto Jan 30 '12 at 13:55