I need some help with this input because I want it only accept 2 decimals and comma or dot as well, but now just allow comma instead of a dot and any decimals. I'm extremely new with RegExp and I was trying with this.
<td>
<input type="number" ng-model="material.porcentaje" ng-change="calculaKilos(material, $index);validatePorcentaje($index)" id="porcentaje" class="input_small-stretch" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
</td>
Check out RegExr to help build your Regular Expression pattern. I could try to build the pattern for you, but without example text it is difficult.
You cannot add , to type="number". You need to change it to type="text" and then onkeyup you can check if there are any values other than numbers and comma you can replace it with ''
document.querySelector('input').addEventListener('keyup',(e)=>{
let value = e.target.value;
e.target.value = value.replace(/[^0-9.,]/g,'');;
})
<input type="text" ng-model="material.porcentaje" ng-change="calculaKilos(material, $index);validatePorcentaje($index)" id="porcentaje" class="input_small-stretch" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
Hope it will help
Related
Hey I have a input where the user enters his social security number. To make the input more readable I want to insert a whitespace after the first 4 characters of the string. The social security number itself is 10 numbers long. So the result should looke like: 1234 567890. I only found solutions where a whitespace every 4 characters is inserted but no example which is similar to this. Has someone an idea how to solve this?
<input type="text" maxlength="10" #keyup="insertWhitespace()"/>
You can do this with the use of Regular Expression + HTML DOM Element addEventListener().
Reference Website For "Regular Expression": https://regexr.com/
With the help of a regular expression is a sequence of characters that specifies a search pattern in text or strings.
document.getElementById('example').addEventListener('input', function (e) {
e.target.value = e.target.value.replace(/[^\dA-Z]/g, '').replace(/(.{4})/, '$1 ').trim();
});
<input id="example" maxlength="11" name="example" />
I think you should make your max length to 11 because white space also counts and try it with the following code
const ssn = document.querySelector("selector");
ssn.addEventListener("keyup",(e) => {
if(e.target.value.length === 4){
ssn.value += " "
}
})
Here is the solution (Javascript) to your problem:
The code below reads the input value and remove alphabets and then replaces the digit value with the appropriate space character only once as expected after 4 digits.
function insertWhitespace() {
document.getElementById('myElement').value = document.getElementById('myElement').value.replace(/[^\dA-Z]/g, '').replace(/(.{4})/, '$1 ').trim()
}
<input id="myElement" maxlength="11" onkeyup="insertWhitespace()" />
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]"});
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}.
I have a long string where I need to increment every number within it, leaving the rest of the text as it is.
I'm using this function
newHtml = newHtml.replace(/\d+/, function (val) { return parseInt(val) + 1; });
Which works great on numbers that are in free text but fails when the numbers are surrounded by square brackets. Example:
<input id="Form[0]_Phone" name="Form[0].Phone" type="text" value="">
Needs to become
<input id="Form[1]_Phone" name="Form[1].Phone" type="text" value="">
I've used this example to try and help, and I've tried a few variations but my regex skills have failed me.
Any assistance much appreciated.
There is nothing in your pattern causing the described behaviour - numbers in square brackets should also be affected. One obvious issue is you're affecting only the first number found, not all - add the g global flag after the closing forward slash of the pattern.
Works for me - see this Fiddle: http://jsfiddle.net/ypUmg/
You need to use 'global' flag, then it should replace all occurences.
i.e.
newHtml = newHtml.replace(/\d+/g, function (val) { return parseInt(val) + 1; });
See it working here:
http://jsfiddle.net/4S7CE/
Without 'g', it would replace only first instance of the match.
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,'')