I want to relpace all non digit char expect ".". dot should be allowed only once. That's mean
4.2 ->ok
a4.21 ->not ok
14.22 ->ok
2.-4 ->not ok
21..43 ->not ok
How can I write regex for this?
I tried /[^0-9.]/g. But multiple dots accept here.
from what you are writing it appears that you want to validate a (potentially decimal) number from some kind of user input.
I would suggest to do it in two steps
trim the input
validate the number
you can trim the input with the regex you mentioned, or with simple mystring.trim() if whitespaces is what you care about.
Then you can use !mystring.isNaN() to validate the correctness of the number (or mystring.parseFloat() if output should be always decimal].
You can use this regex to validate the decimal point numbers.
/^\d*.?\d+$/
Explanation :
^\d* - Matches zero or number of digits.
.?\d+ - Matches an optional decimal point(.) followed by at least one digit.
Live Demo :
function checkValue() {
const inputVal = document.getElementById('numberfield').value;
var regex = /^\d*.?\d+$/
document.getElementById('result').innerText = inputVal.match(regex) ? 'Valid' : 'Not valid'
}
<input type='text' id="numberfield" onblur="checkValue()"/>
<span id="result"></span>
Related
I'm trying to limit an input field to only numbers and the possibility of a "+" sign just at the string's [0] index. The idea is to let the user type their phone number, I have come up with this:
function main() {
let phone = document.getElementById('phone');
let phoneRegex = /[a-zA-Z]|[-!$%^&*()_|~=`{}[\]:";'<>?,./]/;
phone.value = phone.value.replace(phoneRegex, '');
console.log(phone.value);
}
<input id="phone">
<button onclick="main()">Run</button>
The thing is, this works fine, but I want to limit the first character to only a digit or a "+" sign, characters coming from index [1] and on should only be numbers.
I can't come up with an idea on how to solve this
Try this as a starting point (you'll probably want to expand it further to account for total string length, delimiters like -, etc.):
let phoneRegex = /\+?\d+/;
+ in a regular expression has a special meaning, so when we want to match the literal character "+" then we need to escape it. And following it with the ? character makes it optional:
\+?
Next, \d followed by + will match any sequence of one or more digits:
\d+
You can see a visualization of this pattern here.
I want to limit the first character to only a digit or a "+"
sign, characters coming from index [1] and on should only
be numbers.
The regex:
/^\+?\d+$/
means:
From the beginning, one or zero + signs, then one or more numbers until the end.
Note the following regex symbols:
* - zero or more
+ - one or more
? - zero or one
I know I'm late in this game, but adding one more solution for me and other's future references.
I came up with this solution which works for me:
/^(\+|[0-9])?[0-9]{12}$/
Thanks
I have a scenario where i need to identify using match_regex whether the characters given below occurs only once and inputs should not contain any other characters or numbers apart form the given list [+-/*UD]. Should treat each as separate characters.
Made up by only one occurrence of [+,-,/,*,U,D]
Valid cases:
+-/
-UD
/-D
+D/
You can use negative look ahead assertion with captured group value. The following regex will check that each character is following by same or not. Only string with single occurrence will get matched.
^(?:([+\-\/*UD])(?!.*\1))+$
Regex explanation here
Check the demo snippet :
function change(ele) {
var re = /^(?:([+\-\/*UD])(?!.*\1))+$/;
document.getElementById('out').textContent = re.test(ele.value) ? 'Valid' : 'Not Valid';
}
<input type="text" oninput="change(this)" />
<span id="out"></span>
I need regular expression for nopt allowing minus and decimal numbers and zero using javascript. I have code but it is not working. help anyone.
$("#hinizio").keyup(function () {
var match = timeRegex.test($(this).val());
alert(match ? 'matches' : 'does not match');
});
http://jsfiddle.net/9CVCC/45/
Allowing Alphanumeric Characters (But Not 0)
If you wanted to allow for any number alphanumeric characters without minus symbols -, decimal points . or just zero as the string, you could use :
/^(?!0)[\da-zA-Z]+$/i
This will explicitly allow one or more alphanumeric characters, however it will not accept 0 explicitly. You can see an example of it here.
Allowing Only Numeric Characters (But Not 0)
If you wanted to only allow numbers but not 0, you could use :
/^(?!0)\d+$/i
You can see an example of this option here.
Sure!
document.getElementById(target).value = newVal.replace(/[^0-9]/g, "");
The above will remove everything except normal numbers, if you want to keep any special characters, add them to the expression.
Best of all its raw JS, just replace the selector with your current working value from this.
$(this).val();
How to validate a password allows at least 4 digits and 1 character without special character using regular expression?
Password Valid Format: 1234a or 6778B or 67A89 (Without Special Characters Like #$%^&*/)
Maximum Password Length : 5 Characters Long
Can any one help me how to do this?.
Thanks.
The pattern you want
any number of numbers
then a letter
any number of numbers
additional restriction as in all should not be longer than 5 chars
The patter could be this: (using positive lookahead to check the numbers-letter-numbers combination then checking for 5 characters to check length)
/^(?=[0-9]*[a-zA-Z][0-9]*$).{5}$/
This regex should work:
^(?=(.*?[0-9]){4})(?=.*?[a-zA-Z])[0-9A-Za-z]{5}$
Since we are only allowing [0-9A-Za-z]{5} symbols in password there is no need to check for special characters here.
Online Demo: http://regex101.com/r/mB6sS5
Explanation:
(?=(.*?[0-9]){4}) - Lookahead to check for presence of at least 4 numbers
(?=.*?[a-zA-Z]) - Lookahead to check for presence at least 1 letter
[0-9A-Za-z]{5} Only allows symbols in character class with max length=5
^[0-9]{4}[A-Za-z]{1}$
This will let you enter four numbers (0-9) and 1 letter (a-z and A-Z) example 1234a.... hope this is the answer you were looking for.
Below is the javascript code for doing the checking you need
var regex =^(?=(.*?[0-9]){4})(?=.*?[a-zA-Z])[0-9A-Za-z]{5}$;
var input = "1234B";
if(regex.test(input)) {
alert(matches[match]);
} else
{
alert("No matches found!");
}
I'm relative new to RegEx and I've encountered a problem. I want to regex a name. I want it to be max 100 characters, contain at least 2 alphabetic characters and it will allow the character '-'.
I have no problem to only check for alphabetic characters or both alphabetic characters and hyphen but I dont't want a name that potantially can be '---------'.
My code without check for hyphens is
var nameRegExp = /^([a-z]){2,100}$/;
An explanation for the code is appreciated as well.
Thanks!
I guess
/^(?=.*[a-z].*[a-z])[a-z-]{1,100}$/
the lookahead part (^(?=.*[a-z].*[a-z])) checks if there are at least two letters. This pattern ("start of string, followed by...") is a common way to express additional conditions in regexes.
You can limit the number of - by adding a negative assertion, as #Mike pointed out:
/^(?=.*[a-z].*[a-z])(?!(?:.*-){11,})[a-z-]{1,100}$/ // max 10 dashes
however it might be easier to write an expression that would match "good" strings instead of trying to forbid "bad" ones. For example, this
/^[a-z]+(-[a-z]+)*$/
looks like a good approximation for a "name". It allows foo and foo-bar-baz, but not the stuff like ---- or foo----bar----.
To limit the number of - you could add a negative look-ahead, where the number 3 is one more than the maximum number you want to allow
/^(?!(?:[a-z]*-){3,})(?=-*[a-z]-*[a-z])[a-z-]{2,100}$/