This question already has answers here:
Javascript regex phone validation
(3 answers)
Closed 7 years ago.
sorry i know this question might duplicated but i'm really bad in regex, hope someone could help to fix it. How i can apply this regex in my javascript. Any help will be appreciated.
^6?01\d{8}$
My current coding.
function phoneNumber()
{
var mobile = document.mainform.HP_NO.value;
//need condition here if phone number does not match with the regex given
alert("Not a valid Phone Number.");
document.mainform.HP_NO.value = "";
document.mainform.HP_NO.focus();
return false;
///
return true;
}
<input type="text" class="input-name" name="HP_NO" placeholder="e.g (60121234567)" onblur="if(value=='') value = '';phoneNumber();" onfocus="if(value=='') maxlength="11" onPaste="return false"/>
How to make regex for phone number start with 60xxxxxxx
Your regex should be
^60\d{8}$
to check if number starts with 60
Explanation:
^: Starts with
60: Matches literal 60
\d: Matches any number/digit
{8}: Matches 8 times the previous group
$ : Ends with
Visual Representation
How i can apply this regex in my javascript.
You can use test() to check if string satisfies regex.
var regex = /^60\d{8}$/;
if (regex.test(mobile) === false) {
alert("Not a valid Phone Number.");
....
}
Related
This question already has answers here:
Return true/false for a matched/not matched regex
(5 answers)
Closed 3 years ago.
I'm trying to match an entire string against a regex formula. This is for validating if a phone number field is likely correct (just based on allowed characters, anyone can make up an number). I've played with Regex before but never truly understood the nuances that make it powerful.
Below I have my dummy phone number and I have the regex I'm using. As you can see I'm simply comparing the length of the match vs the length of the string and if they match the number must be valid.
Is there a way to get a simple true/false reply from a Regex check on an entire string?
var num = '+1 (888) 456-7896';
var regex = /[0-9+ ()-]*$/;
var found = num.match(regex);
console.log(found[0].length);
console.log(num.length);
You can use test()
var found = regex.test(num);
This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 4 years ago.
I have this code which converts some text to lower case but is it possible to convert to 1st character to a capital letter followed by the remaining characters being lower case?
NWF$(document).ready(function() {
NWF$('#' + varTitle).change(function() {
this.value = this.value.toLowerCase();
});
});
Thanks to Ulysse BN , I got the hint to use the following
this.value = this.value[0].toUpperCase() + this.value.slice(1).toLowerCase()
which works excellent but if you wanted to use VGA as capital then this would not be possible, it is a catch 22 situation. Just wanted to avoid users from typing everything in capital (I hate it).
You could lowercase the whole string except the first letter:
let value = 'HEYhoHAHA'
value = value[0].toUpperCase() + value.slice(1).toLowerCase()
document.write(value)
This question already has answers here:
Match exact string
(3 answers)
Closed 6 years ago.
I'm familiar with Java regex but not Javascript regex.
I want to check the validity of the string input if it looks like a first name. A first name can either be xxxx xxx or xxx where x is a letter. Note that in what I've said, the number of x's vary. So here's my code:
function checkName(name) {
var pattern = new RegExp('([A-Za-z]+|[A-Za-z]+\\s[A-Za-z]+)');
return pattern.test(name);
}
I've made a function that will process the name from the text input in my form. Now it is working for inputs such as Michael or Michael James. However, it also works on M3 for example which has a number and is supposed to be not working. So any help on this one experts out there in the world?
There is no matches method in JavaScript (unlike Java). If you just want to ensure a whole string matches the pattern, add ^ at the start and $ at the end. Also, it is advised to use a regex literal notation /.../ if your regex pattern is static.
Use
var pattern = /^([A-Za-z]+|[A-Za-z]+\s[A-Za-z]+)$/;
^^ ^
To further optimize it (although the performance gain will be tiny), you may use an optional group (?:....)? (see vasan's comment):
var pattern = /^[A-Za-z]+(?:\s[A-Za-z]+)?$/;
^^^^^^^^^^^^^^^^
See the regex demo.
function checkName(name) {
var pattern = /^([A-Za-z]+|[A-Za-z]+\s[A-Za-z]+)$/;
return pattern.test(name);
}
let log = document.querySelector("#log"),
test = document.querySelector("#test");
test.addEventListener("input", e => {
if(checkName(test.value)){
log.style.color = "green";
log.innerHTML = "Passed!";
}else{
log.style.color = "red";
log.innerHTML = "Failed";
}
});
#test:focus{outline: none;}
<input type="text" id="test" autofocus /><br />
<span id="log"></span>
This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
The below code is for validating email. I am getting false for inputs like,
mike#gmail.com
kid#gmail.com
stain#yahoo.com
Can someone point what mistake in the code?
function validate(){
fieldValue = document.getElementById("check").value;
pattern = new RegExp(/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/);
if(pattern.test(fieldValue)){
alert("true");
} else {
alert("false");
}
}
Thanks
A-Z only checks capital letters. Add also a-z:
[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}
Using RegEx to validate email addresses is difficult.
However, the issue with your code is the casing (as others have pointed out). You can fix it by changing A-Z to A-Za-z, which will check for lowercase and capital letters.
function validate(){
fieldValue = document.getElementById("check").value;
pattern = new RegExp(/^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/);
if(pattern.test(fieldValue)){
alert("true");
} else {
alert("false");
}
}
For validating email addresses, this pattern has worked for me for quite some time:
/^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Password validation regex
between 8 and 16 characters, with at least 1 character from each of the 3 character classes -alphabetic upper and lower case, numeric, symbols.
I have this code, but it doesn't work, when I write more than 16 characters, gives it as valid, but it should not; the it should to work ok with 3 character classes, but it works with 4, where's my mistake??
http://jsbin.com/ugesow/1/edit
<label for="pass">Enter Pass: </label>
<input type="text" id="pass" onkeyup="validate()">
Script
function validate() {
valor = document.getElementById('pass').value;
if (!(/(?=.{8,16})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*/.test(valor))) {
document.getElementById('pass').style.backgroundColor = "red";
} else {
document.getElementById('pass').style.backgroundColor = "#adff2f";
}
}
Regular expressions are not a panacea. It's not too hard to do it, mixing with regular code:
function validatePassword(password) {
// First, check the length.
// Please see my comment on the question about maximum password lengths.
if(password.length < 8 || password.length > 16) return false;
// Next, check for alphabetic characters.
if(!/[A-Z]/i.match(password)) return false;
// Next, check for numbers.
if(!/\d/.match(password)) return false;
// Next, check for anything besides those.
if(!/[^A-Z\d]/i.match(password)) return false;
// If we're here, it's valid.
return true;
}
However, I'd look into something like zxcvbn, a password checker, which I think is a better password quality checker, checking things like common dictionary words after un-13375p3/-\kification and dealing with entropy decently. It is used, among others, by Dropbox. Try it here.
You need to anchor the match to the beginning of the string, and anchor the first lookahead to the end:
^(?=.{8,16}$)
Also, the last lookahead needs to be split in two:
(?=.*?[A-Z])(?=.*?[a-z])
Why don't you just test for the three character sets with regular expressions:
[A-Za-z0-9]+
Then count the length of the string to validate the length.
What about this range:
/[A-Za-z0-9$-/:-?{-~!"^_`\[\]]/
So you can check first
/[A-Za-z]+/
then
/\d+/
and finally
/[$-/:-?{-~!"^_`\[\]]+/
If it passes you can check the length.
You can see this link to see why the symbols work.