I'm using happyJS and use the regex underneath for phone validation
phone: function (val) {
return /^(?:[0-9]+$)/.test(val);
}
However this ONLY allows numbers. I want the user to be able to enter spaces as well like
238 238 45383
Any idea why return /^(?:[0-9 ]+$)/.test(val); is not doing the trick?
This is my suggested solution:
/^(?=.*\d)[\d ]+$/.test(val)
The (?=.*\d) asserts that there is at least one digit in the input. Otherwise, an input with only blank spaces can match.
Note that this doesn't put any constraint on the number of digits (only makes sure there are at least 1 digit), or where the space should appear in the input.
Try
phone: function (val) {
return /^(\s*[0-9]+\s*)+$/.test(val);
}
At least one number must be present for the above to succeed but please have a look at the
regex example here
Try
/^[\d ]*$/.test("238 238 45383")
console.log(/^[\d ]*$/.test("238 238 45383"));
You can try the below regex for checking numbers and spaces.
function isTextAndNumberSpaceOnly(text) {
var regex = /^[0-9 ]+$/;
if (regex.test(text)) {
return true;
} else {
return false;
}
}
Personally I use this code and it works properly:
function validateMobile(mob)
{
var re = /^09[0-9]{9}$/
if(mob.match(re))
return true;
else
return false;
}
Related
In my application someone can submit text from another language. I'd like for only English alphanumeric strings to be entered. I've got this JavaScript function working but wondering if this is the best method for doing this?
var string = $('input[name=title]').val();
if((/\d/.test(string) || /[a-zA-Z]/.test(string)) === false) {
alert('Field has no Alphanumeric characters.');
fPass = false;
}
Even if some just enters 1 character I want that to be allowed, as long as it is a number or a character from one of the 26 letters in the English alphabet (case insensitive)
Without using a function here is what I've come up with
if((/[\da-z]/i.test(string)) === false) {
alert('Please use alphanumeric characters.')
}
You can combine your regex into one expression.
function hasAlphanumeric(str) {
return /\d|[A-z]/.test(str)
}
You can use
^[\da-z]+$
let allowAlphanumeric = (str) =>{
return /^[\da-z]+$/i.test(str)
}
console.log(allowAlphanumeric('$##'))
console.log(allowAlphanumeric(''))
console.log(allowAlphanumeric(' '))
console.log(allowAlphanumeric('abchbchdb12e44'))
i need a javascript function that able to check for digit and - only.
example: 1,2,3,4,5,6,7,8,9,0 will return true
and - will return true as well.
other than that all return false including enter is pressed.
i have a function like this:
function IsNumeric(sText){
var filter = /^[0-9-+]+$/;
if (filter.test(sText)) {
return true;
}else {
return false;
}
}
i call it like this:
if(!IsNumeric(value)) {
alert("Number and - only please");
}
for some reason it does not work, any method to do the verification without using regex?
EDIT: OK, updated as per your comment, an expression to match either a lone minus sign or any combination of digits with no minus sign:
function IsNumeric(sText){
return /^(-|\d+)$/.test(sText);
}
If you want only positive numbers and don't want to allow leading zeros then use this regex:
/^(-|[1-9]\d*)$/
Regarding your question "any method to do the verification without using regex?", yes, there are endless ways to achieve this with the various string and number manipulation functions provided by JS. But a regex is simplest.
Your function returns true if the supplied value contains any combination of digits and the plus or minus symbols, including repeats such as in "---+++123". Note that the + towards the end of your regex means to match the preceding character 1 or more times.
What you probably want is a regex that allows a single plus or minus symbol at the beginning, followed by any combination of digits:
function IsNumeric(sText){
return /^[-+]?\d+$/.test(sText);
}
? means match the preceding character 0 or 1 times. You can simplify [0-9] as \d. Note that you don't need the if statement: just return the result from .test() directly.
That will accept "-123", "123", "+123" but not "--123". If you don't want to allow a plus sign at the beginning change the regex to /^-?\d+$/.
"example: 1,2,3,4,5,6,7,8,9,0 will return true and - will return true as well."
Your example seems to be saying that only a single digit or a single minus sign is considered valid - if so then try this:
function IsNumeric(sText){
return /^[\d-]$/.test(sText);
}
How about
function IsNumeric(s) {
return /^(+|-|)\d*$/.test(s);
}
Hiphen(-) has special meaning so use escape character in character set.
Try this:
var filter = /^[0-9\-]+$/;
Can be simple ... try this:
function IsNumeric(str) {
return str.length == 1 && (parseInt(str) < 10 || str == "-");
}
Trying to get the correct regex for this - only letters, spaces, hypens, and commas. So far this only works if you only input 1 charactor. Any more then that, and it returns false. Anyone able to help?
$('#submit').click(function () {
var locationtest = /[^a-zA-Z \-\.\,]/;
if (!locationtest.test($('#location').val())) {
alert('Nope, try again!');
$('#location').val('')
return false;
} else {
alert('You got it!');
}
});`
This should do it, it matches 1 or more characters within the set you described
/^[a-zA-Z \-\,]+$/
I took out the \., your description says letters, spaces, hyphens, commas
You're close, you just need to specify how many times you want the character to appear.
The following code would specify 0 or more times
var locationtest = /[^a-zA-Z -.\,]*/;
And this code would specify 1 or more times
var locationtest = /[^a-zA-Z -.\,]+/;
The importance being the * and + characters.
Add a quantifier + and the global flag /g:
var locationtest = /[^a-zA-Z \-\.\,]+/g;
Your expression is correct, you just need to invert the match result.
/[^a-zA-Z \-\.\,]/
Will match if the string contains any char that is not what you want (the leading ^ in the character class).
I.e remove the !:
var locationtest = /[^a-zA-Z \-\.\,]/;
if (locationtest.test($('#location').val())) {
alert('Nope, try again!');
$('#location').val('')
return false;
} else {
alert('You got it!');
}
Note that empty string will pass as valid, if you don't want that, you can use this instead:
/[^a-zA-Z \-\.\,]|^$/
I am having a problem to get the simple reges for alphanumeric chars only work in javascript :
var validateCustomArea = function () {
cString = customArea.val();
var patt=/[0-9a-zA-Z]/;
if(patt.test(cString)){
console.log("valid");
}else{
console.log("invalid");
}
}
I am checking the text field value after keyup events from jquery but the results are not expected, I only want alphanumeric charachters to be in the string
This regex:
/[0-9a-zA-Z]/
will match any string that contains at least one alphanumeric character. I think you're looking for this:
/^[0-9a-zA-Z]+$/
/^[0-9a-zA-Z]*$/ /* If you want to allow "empty" through */
Or possibly this:
var string = $.trim(customArea.val());
var patt = /[^0-9a-z]/i;
if(patt.test(string))
console.log('invalid');
else
console.log('valid');
Your function only checks one character (/[0-9a-zA-Z]/ means one character within any of the ranges 0-9, a-z, or A-Z), but reads in the whole input field text. You would need to either loop this or check all characters in the string by saying something like /^[0-9a-zA-Z]*$/. I suggest the latter.
I fixed it this way
var validateCustomArea = function () {
cString = customArea.val();
console.log(cString)
var patt=/[^0-9a-zA-Z]/
if(!cString.match(patt)){
console.log("valid");
}else{
console.log("invalid");
}
}
I needed to negate the regex
i try to allow only number 01 (1) to 53) after / and after 2000 and over....
so i create a regex but it don't seem to work
on this web page: http://www.regular-expressions.info/javascriptexample.html
i tried it and it work well... but when i test in on a web page
10/2010 , 23/2000
function isValidDate(value, format){
var isValid = true;
try{
var inputVal = $(this).val();
var dateWWYYYYRegex = '^(0[1-9]|[1234][0-9]|5[0-3])[-/.](20)\d\d$';
var reg=new RegExp(dateWWYYYYRegex);
if(!reg.test(value)){
isValid = false;
alert("Invalid");
}
}
catch(error){
isValid = false;
}
return isValid;
}
You have to escape backslashes if you're going to make a regex from a string. I'd just use regex syntax, since it's a constant anyway:
var reg = /^(0[1-9]|[1234][0-9]|5[0-3])[-/.](20)\d\d$/;
The regular expression doesn't really make any sense, however. It's not clear what it should be, because your description is also confusing.
edit — OK now that I see what you're doing, that regex should work, I guess.
Why use regex for this task? I think it's the wrong tool for this task
Simply split the string by the slash delimiter, and then use numerical functions to check if the values are in the range you want.
function isValidWeekOfYear(value){
var bits = value.split('/');
if(parseInt(bits[1]) < 2000) { return false; } /* probably also want to do a maximum value here? */
if(parseInt(bits[0]) < 1 || parseInt(bits[0]) > 53) { return false; }
return true;
}
It might need a bit more validation than that, but that should be a good starting point for you. Much less processing overhead than a regex just to parse a couple of numbers (and easier to read too).