Is there a way to set a min and max length of 15 numbers (numbers only) in my form field?
I've tried:
pattern".{15,15}"
Here's the JSFiddle: http://jsfiddle.net/aScpD/2/
Try
\d --> for numbers
Fiddle Demo
pattern="\d{15,15}"
Update as commented by s1lence
pattern="\d{15}"
Update to set Custom validation message
Add this attribute to input field
oninvalid="this.setCustomValidity('15 digit number needed')"
Use as commented by Jukka K. Korpela (check above fiddle for working demo)
title="Exactly 15 digits"
You can also do it like this.
<input title="requires exactly 15 digits" maxlength="15" pattern=".{15}" required />
Demo
Try this:
pattern = /[0-9]{15}/
[0-9] match a number (safer than \d because \d matches also
foreign number)
Edit: According to Bergi, it's not true in JS, but it's still valid in a lot of Perl RegEx implementations.
{15} is similar to {15, 15}.
Related
In jquery input mask,
We can specify number and letter. How to get the format where user can input letter or number,
A = Letter,
9 = number.
I need a mask like this AA - (A or 9)(A or 9) - 99,
For example, it can be AA-99-99
and in another case it can be AA-A9-99, or
AA-AA-99,
and so on. I have tried a lot, not suitable.
Please advise.
As you noted * stands for alphanumeric character, so AA-**-99 should do the job.
Or you could use regexp lik [A-Za-z]{2}-\w{2}-\d{2}, but it looks like regex placeholders like \w are not supported like this. Enclosing it to square brackets works ok:
<input id="example2" data-inputmask-regex="[A-Za-z]{2}-[\w]{2}-\d{2}" />
Here is a working plunker: https://plnkr.co/edit/SDkFm5VChO7W19VuOXmK?p=preview
From the docs, you can use this method.
The regex AA-([A]|[9])([A]|[9])-99 will match your situation of numbers/letters.
HTML
<input id="example2" data-inputmask-regex="AA-([A]|[9])([A]|[9])-99" />
Javascript
(function() {
$("#example2").inputmask();
})();
If by A you mean exactly the letter A and by 9 exactly the number 9, then this should work for you:
$("#myinput").inputmask({mask:"[AA]-[A|9]{2}-[99]"});
Hi this is working now but I am confused at to why.
I am learning regex and need to pull the numbers out of strings like
'Discount 7.5%' should get 7.5
'Discount 15%' should get 15
'Discount 10%' should get 10
'Discount 5%' should get 5
etc.
/\d,?.\d?/ //works
/\d,?.\d,?/ //doesn't works
/\d?.\d?/ //doesn't works
I thought one of the second two would work could someone explain this.
Quick and dirty with easy to understand regex.
//Let the system to optimize the number parsing for efficiency
parseFloat(text.replace(/^\D+/, ""));
Demo: http://jsfiddle.net/DerekL/4bnp8381/
Try this. Make the second part with dot . optional with ?
(\d)*(\.\d*)?
IF you check this Regex, this is what you need so it will return the float number only if the word before it is Discount by making use of the lookbehind operator:
(?<=Discount\s)\d+(\.\d+)?
But the problem is that works with PHP(prce) I can't get it working with javascript.
EDIT:
As mentioned in here regex and commas Javascript does not support lookbehind regex so here is what I did to work around it JS Fiddle
var div = document.getElementById('test');
var text = div.innerHTML;
div.innerHTML = text.replace(/Discount (.*)%/ig,'$1');
<div id="test">
Discount 7.5%<br>
Discount 15%<br>
discount 10%<br>
Discount 5.433%<br>
Fee 11%<br>
Discount 22.7%
</div>
As you see it does match it only if it was followedd by the word Discount, the word Fee 11% does not match
You can use the following regex, which will match one or more digits, optionally followed by a decimal point and any number of digits:
^Discount\s(\d+(\.\d+)?)%$
Regex101
I want to allow only integers and floats (upto 3 decimal places) in a text box, how can I achieve this using javascript?
Valid values are
1234
12.3
12.314
1.11
0.4
Not valid
1.23456
abcd or any other character
Based on the comment that you need to also match ".1" you need to add a conditional with the first part of the regular expression.
var re = /^(\d+)?(?:\.\d{1,3})?$/;
Rough test suite - jSFiddle
You can use a regular expression to do this:
/^\d+(?:\.\d{1,3})?$/
That's the start of the string (^), one or more digits (\d+), optionally followed by a . and between 1 and 3 digits ((?:\.\d{1,3})), then the end of the string ($).
To compare it to the value of an input, you'd do something like this:
var re = /^\d+(?:\.\d{1,3})?$/;
var testValue = document.getElementById('id-of-input').value;
if(re.test(testValue)) {
// matches - input is valid
}
else {
// doesn't match - input is invalid
}
Take a look at this jsFiddle demo.
use regular expression to validate your input field , regular rexpression is as below
^[0-9]+(?:\.[0-9]{1,3})?$
Try this:
var reg=/^[\d]+(?:\.\d{1,3})?$/;
str=10.2305;
str1=123;
alert(reg.test(str));
alert(reg.test(str1));
Check Fiddle http://jsfiddle.net/8mURL/1
I need to check Format Phone Number.
+33xxxxxxxxx
0033xxxxxxxxx
EDIT : 0xxxxxxxxx
How can I do that with(out) regex ?
The regex to match it would be this
(0033|\+33|0)?\d{9}
I use http://regexpal.com/ for quick regex testing
if (/^(?:(?:\+|00)33|0)\d{9}$/.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
PhoneFormat.com has a javascript library that has some formatting functions that you could easily drop into your project. It will take whatever number you throw at it and try and convert it to e164 (+ 33252525252), and also format it (+33 2 52 52 52 52)
Try this regex: /^((\+|00)\d{2})?\d{9}$/
This matches each of your given cases (+YYXXXXXXXXX, 00YYXXXXXXXXX, XXXXXXXXX).
Edit: To match your edit: /^((\+|00)\d{2}|0)\d{9}$/
This is a regex that validates your examples:
/(\+|(00)|0)[0-9]{11}/.test(stringToTest)
This works:
var.replace(/[^0-9]+/g, '');
That simple snippet will replace anything that is not a number with nothing.
But decimals are real too. So, I'm trying to figure out how to include a period.
I'm sure it's really simple, but my tests aren't working.
Simply: var.replace(/[^\d.-]+/g, '');
Replacing something that is not a number is a little trickier than replacing something that is a number.
Those suggesting to simply add the dot, are ignoring the fact that . is also used as a period, so:
This is a test. 0.9, 1, 2, 3 will become .0.9123.
The specific regex in your problem will depend a lot on the purpose. If you only have a single number in your string, you could do this:
var.replace(/.*?(([0-9]*\.)?[0-9]+).*/g, "$1")
This finds the first number, and replaces the entire string with the matched number.
Try this:
var.replace(/[^0-9\\.]+/g, '');
there's a lot of correct answers already, just pointing out that you might need to account for negative signs too.. "\-" add that to any existing answer to allow for negative numbers.
Try this:
var.replace(/[0-9]*\.?[0-9]+/g, '');
That only matches valid decimals (eg "1", "1.0", ".5", but not "1.0.22")
If you don't want to catch IP address along with decimals:
var.replace(/[^0-9]+\\.?[0-9]*/g, '');
Which will only catch numerals with one or zero periods
How about doing this:
var numbers = str.gsub(/[0-9]*\.?[0-9]+/, "#{0} ");
Sweet and short inline replacing of non-numerical characters in the ASP.Net Textbox:
<asp:TextBox ID="txtJobNo" runat="server" class="TextBoxStyle" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" />
Alter the regex part as you'ld like. Lots and lots of people complain about the cursor going straight to the end when using the arrow keys, but people tend to deal with this without noticing it for instance, arrow... arrow... arrow... okay then... backspace back space, enter the new chars.
Here are a couple of jQuery input class types I use:
$("input.intgr").keyup(function (e) { // Filter non-digits from input value.
if (/\D/g.test($(this).val())) $(this).val($(this).val().replace(/\D/g, ''));
});
$("input.nmbr").keyup(function (e) { // Filter non-numeric from input value.
var tVal=$(this).val();
if (tVal!="" && isNaN(tVal)){
tVal=(tVal.substr(0,1).replace(/[^0-9\.\-]/, '')+tVal.substr(1).replace(/[^0-9\.]/, ''));
var raVal=tVal.split(".")
if(raVal.length>2)
tVal=raVal[0]+"."+raVal.slice(1).join("");
$(this).val(tVal);
}
});
intgr allows only numeric - like other solutions here.
nmbr allows only positive/negative decimal. Negative must be the first character (you can add "+" to the filter if you need it), strips -3.6.23.333 to -3.623333
I'm putting nmbr up because I got tired of trying to find the way to keep only 1 decimal and negative in 1st position
This one just worked for -ve to +ve numbers
<input type="text" oninput="this.value = this.value.replace(/[^0-9\-]+/g, '').replace(/(\..*)\./g, '$1');">
I use this expression to exclude all non-numeric characters + keep negative numbers with minus sign.
variable.replace(/[^0-9.,\-]/g,'')