field validation for string along with number in javascript - javascript

I have a password field in one form. Now I have to validate in such a way that the field value should be a 7 digits string along with a number. Otherwise it will return false.
Please help me.

Create regex first
Var regex = /\w{7}\d/i;
var yourvalue=$("#passwordid").value;
regex.test(yourvalue){
return true;
}
else{
return false
}

I’m sure there is a better way, but something like:
if ( /.{7}/.test(str) && /\d/.test(str) ) {
//OK
}

In your javascript you can use the RegExp object.
var regEx = new RegExp(pattern, modifiers);
or more simply:
var pattern = /pattern/modifiers;
E.g.
var password = "abcdefg1";
var pattern = /\w{7}\d/i;
var isMatch = pattern.test(password);
Here are some expressions:
[abc] Find any character between the brackets
[^abc] Find any character not between the brackets
[0-9] Find any digit from 0 to 9
[A-Z] Find any character from uppercase A to uppercase Z
[a-z] Find any character from lowercase a to lowercase z
[A-z] Find any character from uppercase A to lowercase z
[adgk] Find any character in the given set
[^adgk] Find any character outside the given set
(red|blue|green) Find any of the alternatives specified
Metacharacters:
. Find a single character, except newline or line terminator
\w Find a word character
\W Find a non-word character
\d Find a digit
\D Find a non-digit character
\s Find a whitespace character
\S Find a non-whitespace character
\b Find a match at the beginning/end of a word
\B Find a match not at the beginning/end of a word
\0 Find a NUL character
\n Find a new line character
\f Find a form feed character
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx Find the character specified by an octal number xxx
\xdd Find the character specified by a hexadecimal number dd
\uxxxx Find the Unicode character specified by a hexadecimal number xxxx
Quantifiers
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{X} Matches any string that contains a sequence of X n's
n{X,Y} Matches any string that contains a sequence of X to Y n's
n{X,} Matches any string that contains a sequence of at least X n's
n$ Matches any string with n at the end of it
^n Matches any string with n at the beginning of it
?=n Matches any string that is followed by a specific string n
?!n

Related

positive lookahead

Use lookaheads to match a string that is greater than 5 characters long and have two consecutive digits.
I know the solution should be
/(?=\w{6,})(?=\D*\d{2})/
But why the second element is
(?=\D*\d{2})
Instead of
(?=\d{2})
Please help me to understand this.
Actually, /(?=\w{6,})(?=\D*\d{2})/ does not ensure there will be a match in a string with 2 consecutive digits.
Check this demo:
var reg = /(?=\w{6,})(?=\D*\d{2})/;
console.log(reg.test("Matches are found 12."))
console.log(reg.test("Matches are not found 1 here 12."))
This happens because \D* only matches any non-digit chars, and once the \w{6,} matches, (?=\D*\d{2}) wants to find the two digits after any 0+ digits, but it is not the case in the string.
So, (?=\w{6,})(?=\D*\d{2}) matches a location in the string that is immediately followed with 6 or more word chars and any 0+ non-digit chars followed with 2 digits.
The correct regex to validate if a string contains 6 or more word chars and two consecutive digits anywhere in the string is
var reg = /^(?=.*\w{6,})(?=.*\d{2})/;
Or, to support multiline strings:
var reg = /^(?=[^]*\w{6,})(?=[^]*\d{2})/;
where [^] matches any char. Also, [^] can be replaced with [\s\S] / [\d\D] or [\w\W].
And to match a string that is greater than 5 characters long and have two consecutive digits you may use
var reg = /^(?=.*\d{2}).{5,}$/
var reg = /^(?=[\s\S]*\d{2})[\s\S]{5,}$/
where
^ - start of string
(?=[\s\S]*\d{2}) - there must be two digits anywhere after 0+ chars to the right of the current location
[\s\S]{5,} - five or more chars
$ - end of string.
The lookahead has to allow the 2 digits anywhere in the input. If you used just (?=\d{2}) then the 2 digits would have to be at the beginning.
You could also use (?=.*\d{2}). The point is that \d{2} has to be preceded by something that can match the rest of the input before the digits.

RegEx pattern test is failing in javascript

I have below RegEx to validate a string..
var str = "Thebestthingsinlifearefree";
var patt = /[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?#\[\]^_`{|}~]*/g;
var res = patt.test(str);
the result will always give true but I thought it would give false.. because I checking any pattern which is not in the patt variable...
The given string is valid and it contains only Alphabets with capital and small case letters. Not sure what is wrong with the pattern.
Here's your code:
var str = "Thebestthingsinlifearefree";
var patt = /[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?#\[\]^_`{|}~]*/g;
console.log(patt.test(str));
The regex
/[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?#\[\]^_`{|}~]*/g
will match anything since it accepts match of length 0 due to the quantifier *.
Just add anchors:
var str = "Thebestthingsinlifearefree";
var patt = /^[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?#\[\]^_`{|}~]*$/;
console.log(patt.test(str));
Here's an explanation or your regex:
[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?#\[\]^_`{|}~]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
A-Z a single character in the range between A and Z (case sensitive)
a-z a single character in the range between a and z (case sensitive)
! a single character in the list ! literally
\\ matches the character \ literally
#$%&()*+, a single character in the list #$%&()*+, literally (case sensitive)
\- matches the character - literally
. the literal character .
\/ matches the character / literally
:;<=>?# a single character in the list :;<=>?# literally (case sensitive)
\[ matches the character [ literally
\] matches the character ] literally
^_`{|}~ a single character in the list ^_`{|}~ literally
Note that:
A search for a missing pattern is better expressed by a negative condition in code (!patt.test...).
You need to escape certain characters like ., (, ), ?, etc. by prefixing them with a backslash (\).
var str = "Thebestthingsinlifearefree";
var patt = /[0-9A-Za-z !\\#$%&\(\)*+,\-\.\/:;<=>\?#\[\]^_`\{|\}~]/;
var res = !patt.test(str);
console.log(res);
This will print false, as expected.

Character matching

I need to match a string to have words and not numbers, I need to match special characters such as $!:{}_ if they are part of the word but ignore otherwise.
I have a regex that matches for word and ignores numbers but cannot work out how to match special characters if they are part of the word but ignore otherwise.
Here is what I have correctly - /^d\s+/
Any help would be appreciated.
Allow words and letters only, and ignore any numbers. Special
characters such as (-_‘[]{}“£$&%!:;/) should either be ignored or
treated as part of the word they sit within.
Try using String.prototype.replace() with RegExp /\s\d+.*\d|\s+[^a-z]/ig to replace space character followed by digit followed by any character followed by digit , or space character followed by any character not a-z case insensitive
var str = "This is a test of 1,2,3 word-count - test.";
str = str.replace(/\s\d+.*\d|\s+[^a-z]/ig, "");
document.body.textContent = str;

validation format of javascript

I found these javascript validation codes:
<script type="text/javascript">
function validasi_input(form){
pola_username=/^[a-zA-Z0-9\_\-]{6,100}$/;
if (!pola_username.test(form.username.value)){
alert ('Username minimal 6 karakter dan hanya boleh Huruf atau Angka!');
form.username.focus();
return false;
}
return (true);
}
</script>
I want to ask about this part:
pola_username=/^[a-zA-Z0-9\_\-]{6,100}$/;
does anyone can tell me how to understand this kind of format? is it format for letter, or number, or characters?
/^[a-zA-Z0-9\_\-]{6,100}$/;
In english this means: that a string can have any letter either uppercase or lowercase, numbers, underscores, and hyphens. A minimum length of 6 characters, and a maximum length of 100.
Further details:
The string must start with either a letter, number, underscore, or hyphen.
/^[a-zA-Z0-9\_\-]{6,100}$/
^ asserts that we are at the beginning of the string
[a-zA-Z0-9_-] string can have any letter either uppercase , lowercase, numbers, underscores, or hyphens.
{6,100} matches a length of character having from 6 to 100
$ asserts that we are at the end of the string
Various RegEx explanation/testing tools.
1. Explain RegEx
2. RegEx101
3. Debuggex Demo
^[a-zA-Z0-9\_\-]{6,100}$
^ is an anchor. It asserts position at start of the string
[a-zA-Z0-9\_\-]{6,100} match a single character present in the list below
{6, 100}: Between 6 and 100 times, as many times as possible, giving back as needed
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
\_ matches the character _ literally
\- matches the character - literally
$ is an anchor. It asserts position at end of the string.
An alternative regex using flags would be:
/^[a-z\d\_\-]{6,100}$/i
Here \d matches digits (0-9), and flag i denotes case insensitivity.
This is what regular expressions do to perform matches, for starters:
(source: gyazo.com)

javascript regex matching 3 digits and 3 letters

How to match word in string that contain exactly "3 digits and 3 letters"?
e.g. 100BLA
var regex = ?;
var string = "word word 100BLA word";
desiredString = string .match(regex);
\d matches a digit
[a-zA-Z] matches a letter
{3} is the quantifier that matches exactly 3 repetitions
^ Anchor to match the start of the string
$ Anchor to match the end of the string
So if you use all this new knowledge, you will come to a regex like this:
^\d{3}[a-zA-Z]{3}$
Update:
Since the input example has changed after I wrote my answer, here the update:
If your word is part of a larger string, you don't need the anchors ^ and $ instead you have to use word boundaries \b.
\b\d{3}[a-zA-Z]{3}\b
INITIAL (incomplete)
var regex = /[0-9]{3}[A-Za-z]{3}/;
EDIT 1 (incomplete)
var regex = /[0-9]{3}[A-Za-z]{3}\b/; // used \b for word boundary
EDIT 2 (correct)
var regex = /\b[0-9]{3}[A-Za-z]{3}\b/; // used \b at start and end for whole word boundary

Categories

Resources