Skip to content Skip to sidebar Skip to footer

Javascript RegEx Partial Match

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

Solution 1:

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.


Solution 2:

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


Solution 3:

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

Solution 4:

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


Solution 5:

According to your last edit, this should work:

/^#[a-fA-F0-9]{0,6}$/

Post a Comment for "Javascript RegEx Partial Match"