We have a regular expression given below:
/[^0-9\\.()]/g
However, it does not accepts user to enter 0, e.g. it says that 0.06 is invalid. I'd like to make such inputs also valid.
All we need is the above regular expression to accept 0 (zero) too.
Try this regex by removing the ^:
/[0-9.()]/g
You are trying to use the same regular expression for input sanitizing and validation. You cannot use the same regex to do both tasks.
After sanitizing, you can add validation step like this:
/^\d*(?:\.\d+)*$/
Sample code:
var str = 'abc0.06abc';
var newstr = str.replace(/[^0-9.()]/g, '');
if (/^\d*(?:\.\d+)*$/.test(str) == false) {
console.log(newstr + " is valid");
}
else {
console.log(newstr + " is not valid");
}
See demo
Thanks Drake. The link which you provided did the trick.
var str = '0.05';
var newstr = str.replace(/[^0-9\.()]/g, '');
console.log(newstr);
Related
I am at a lost as to why this will not.
here is my regular expression:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|`¬':;~{}<>()#?!#$%^&*-]).{8,20}$
here is some code to simply test it:
var str1 = "AAbb123.";
var str2 = "ell";
var re = new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$");
if(str1.match(re)){
alert("matched")
}
else {
alert("doesnt match")
}
the regular expression has been validated in 2 regular expression web sites (regexpal.com & http://www.freeformatter.com/regex-tester.html). both say str1 is valid for this expression but yet when included in my code it will not work.
below is another place I am trying to get the code working. and it keeps printing: requirements not met.
var uname = document.getElementById("pword1").value;
var re = new RegExp ("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$");
if(uname.match(re)){
DIMR = "Requirements MET";
}else {
DIMR = "Requirements NOT MET";
}
You need to properly escape a string when using new RegExp constructor.
Since you don't have any variables inside your pattern try
var str1 = "AAbb123.";
var str2 = "ell";
var re = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$/;
if(str1.match(re)){
alert("matched")
}
else {
alert("doesnt match")
}
Escaping only few characters present inside the character class would be enough. When using " as regex delimiter, you need to escape the backslash in your regex one more time.
var re = new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\\+=.\\[\\]_£|`¬':;~{}<>()#?!#$%^&*-]).{8,20}$");
special characters like +, ., * inside a character class would must match a literal + or . or *, so you don't need to escape it. To match a literal \, you need to escape that \ exactly three times.
I am trying to only allow alphanumeric entry or these characters:'()-_. (with the "." included)
Using regexpal.com I entered this regular expression: [^a-zA-Z0-9()\.'\-\_ ]
It is correctly identifying * and # as a match. What's baffling is that I have that same exact expression in my javascript on an .aspx page and it is not catching * or #. I have confirmed that is indeed entering that function and that the expression evaluates. Here is that code:
$(".validateText").keyup(function (e) {
var matchPattern = "[^a-zA-Z0-9()\.'\-\_ ]";
var regEx = new RegExp(matchPattern);
console.log("Regex: " + regEx + "\nValue of " + e.target.id + " is: " + e.target.value);
if (regEx.test(e.target.value)) {
console.log("Found invalid data.");//I don't get here with # or *
var failingChar = e.target.value.length - 1;
e.target.value = e.target.value.substring(0, failingChar);
}
});
Rather than using string literals to define regexes, use regex literals.
var regEx = /[^a-zA-Z0-9()\.'\-\_ ]/;
String literals interpret backslashes as escape characters, so they need to be escaped. Regex literals don't require this.
As per Bergi's suggestion, you wouldn't even need to escape all those characters.
/[^a-zA-Z0-9().'_ -]/
You could probably even use the general \w character.
/[^\w().' -]/
var matchPattern = "[^a-zA-Z0-9()\\.'\\-\\_ ]";
Would work.
I have a form with an input that follows this pattern:
pattern='(\+|00)\d{2,3}[-]\d{8,10}'
an example would be +999-123456789
I have to form validate it again using javascript and have tried to convert the pattern into a Regex, the example passes the pattern but fails passing the regex. Any idea as to why?
var check = /^([00|+])([0-9]{2,3})[-]?([0-9]{8,10})$/;
Your regular expression is incorrect. This:
[00|+]
is equivalent to
[0|+]
and means "match a single character that's either '0', '|', or '+'." I think you want:
var check = /^(00|\+)(\d{2,3})-(\d{8,10)$/;
Here is your pattern tranferred to a RegEx: /(\+|00)\d{2,3}-{0,1}\d{8,10}$/. Example below.
var number = '+999-123456789';
if (number.match(/(\+|00)\d{2,3}-{0,1}\d{8,10}$/)) {
alert('Phone number valid!');
} else {
alert('Phone number invalid.');
}
I want to remove special characters from the starting of the string only.
i.e, if my string is like {abc#xyz.com then I want to remove the { from the starting. The string shoould look like abc#xyz.com
But if my string is like abc{#xyz.com then I want to retain the same string as it is ie., abc{#xyz.com.
Also I want to check that if my string has # symbol present or not. If it is present then OK else show a message.
The following demonstrates what you specified (or it's close):
var pat = /^[^a-z0-9]*([a-z0-9].*?#.*?$)/i; //pattern for optional non-alphabetic start followed by alphabetic, followed by '#' somewhere
var testString = "{abc#xyz.com"; //Try with {abcxyz.com for alert
arr = pat.exec(testString);
var adjustedString;
if (arr != null) { adjustedString = arr[1]; } //The potentially adjustedString (chopped off non-alphabetic start) will be in capture group 1
else { adjustedString = ""; alert(testString + " does not conform to pattern"); }
adjustedString;
I have used two separate regex objects to achieve what you require .It checks for both the conditions in the string.I know its not very efficient but it will serve your purpose.
var regex = new RegExp(/(^{)/);
var regex1 = new RegExp(/(^[^#]*$)/);
var str = "abc#gmail.com";
if(!regex1.test(str)){
if(regex.test(str))
alert("Bracket found at the beginning")
else
alert("Bracket not found at the beginning")
}
else{
alert("doesnt contain #");
}
Hope this helps
Thanks in advance.
I would like a regular expression that removes anything that is NOT alpha numeric and a hyphen. So allowed are A-Z 0-9 and -.
Also, how could I apply that to a string in Javascript?
Thanks again.
var str = 'a23a-asd!##$';
str.replace(/[^-a-z0-9]/ig,'');
Try this:
str = str.replace(/[a-zA-Z\d-]/g, "");
var output = input.replace(/[^A-Za-z0-9-]/g, "");
string.replace(/[^a-zA-Z0-9-]/g, "");
$(document).ready(function (e) {
$('#Box, #Window').keyup(function(){
var $th = $(this);
$th.val($th.val().replace(/[^a-zA-Z0-9]/g, function(str){return '';}));
});
});
This can be used to validate Alphanumeric values.