Form Validaton Code Not Working - javascript

var val_regex = \d{3}+(\.[0-9]{1,2})?$
function validate(form){
var valOne = form.valone.value;
var valTwo = form.valtwo.value;
var valThree = form.valthree.value;
if (!val_regex.test(valOne)) || (!val_regex.test(valTwo)) ||(!val_regex.test(valThree)){
$("#val-error").html("Error with inputted value");
$('#val-error').fadeIn(200).delay(1500).fadeOut(800);
return false;
}
return true;
}
}
My regex expression here doesn't seem to be working. Can you see what the problem is?
Regex expression - what I want - any number between 0 and 999 (inclusive) - there is a maximum of 2 decimal places allowed. Am I writing the regex expression the wrong way or is my javascript wrong?

Your regular expression doesn't match single, or double digit numbers.
Try this:
var val_regex = /^(([0-9])|([1-9][0-9])|([1-9][0-9][0-9]))(\.[0-9]{1,2})?$/;

var val_regex = /^\d{1,3}(\.\d{1,2})?$/;
Explanation:
\d{1,3}
Allow 1-3 digits.
\d{1,3}(\.\d{1,2})?
Allow 1-3 digits and, optionally, allow a period and 1-2 more digits.
^\d{1,3}(\.\d{1,2})?$
All of the above, but allow nothing else before or after it. Make sure you .trim() your inputs to eliminate any whitespace before and after.
This will allow "0" - "999". It will also allow "0.0", "0.00" and "999.99" but not "0." (must have at least digit after the decimal place).

Related

Javascript regex for money with max length

I want validate a money string with numbers max length 13 with 2 decimal. I have a comma as decimal separator and a period as a thousands separator.
I have this regex:
/^(\d{1}\.)?(\d+\.?)+(,\d{2})?$/
For sintax is valid but not for max length. What I need to add to this regex?
For example, these strings must be valid:
1.000.000.000.000
1.000.000.000.000,00
1
1,00
123,45
And these must be invalid:
10.000.000.000.000
10.000.000.000.000,00
10.000.000.000.000.000
10.000.000.000.000.000,00
Maybe try to assert position is not followed by 18 digits/dots using a negative lookahead:
^(?![\d.]{18})\d{1,3}(?:\.\d{3})*(?:,\d\d?)?$
See an online demo. Here is assumed you would also allow a single digit decimal.
^ - Open line anchor.
(?![\d.]{18}) - Negative lookahead to prevent 18 digits/dots ahead.
\d{1,3} - One-to-three digits.
(?:\.\d{3})* - A non-capture group of a literal dot followed by three digits with a 0+ multiplier.
(?:,\d\d?)? - Optional non-capture group of a comma followed by either 1 or two digits. Remove the question mark to make the 2nd decimal non-optional.
$ - End line anchor.
You may use this regex for validation:
^(?=[\d.]{1,17}(?:,\d{2})?$)\d{1,3}(?:\.\d{3})*(?:,\d{2})?$
RegEx Demo
RegEx Details:
^: Start
(?=[\d.]{1,17}(?:,\d{2})?$): Lookahead to match dot or digit 1 to 17 times followed by optional comma and 2 digits
\d{1,3}: Match 1 to 3 digits
(?:\.\d{3})*: Match . followed by 3 digits. Repeat this group 0 or more times
(?:,\d{2})?: Match optional , followed 2 decimal digits
$: End
90% of the time there is a better solution than using regex. It's probably best just to convert your strings into a real number then compare vs. your limit (ie 9999999999999.99).
// Pass a string
function convertStr(string) {
/*
Remove all '.'
Replace all ',' with '.'
*/
let numStr = string.split('.').join('').replaceAll(',', '.');
// Convert modified string into a real number
let realNum = parseFloat(numStr);
/*
if converted string is a real number...
round it to two decimal places and return it
Otherwise return false
*/
return !Number.isNaN(realNum) ? Math.round((realNum + Number.EPSILON) * 100) / 100 : false;
}
// Pass a string and the maxed number
function numLimit(string, limit) {
// Get the result of convertString()
let number = convertStr(string);
// if the result is equal to or less than limit...
if (number <= limit) {
return true;
} else {
return false;
}
}
const limit = 9999999999999.99;
const valid = ['1.000.000.000.000',
'1.000.000.000.000,00', '1', '1,00', '123,45'
];
const invalid = ['10.000.000.000.000', '10.000.000.000.000,00', '10.000.000.000.000.000', '10.000.000.000.000.000,00'];
let validResults = valid.map(str => numLimit(str, limit));
let invalidResults = invalid.map(str => numLimit(str, limit));
console.log('valid: ' + validResults);
console.log('invalid: ' + invalidResults);

Regex for getting only the last N numbers in javascript

I've being trying to generate a regex for this string:
case1: test-123456789 should get 56789
case2: test-1234-123456789 should get 56789
case3: test-12345 should fail or not giving anything
what I need is a way to get only the last 5 numbers from only 9 numbers
so far I did this:
case.match(/\d{5}$/)
it works for the first 2 cases but not for the last one
You may use
/\b\d{4}(\d{5})$/
See the regex demo. Get Group 1 value.
Details
\b - word boundary (to make sure the digit chunks are 9 digit long) - if your digit chunks at the end of the string can contain more, remove \b
\d{4} - four digits
(\d{5}) - Group 1: five digits
$ - end of string.
JS demo:
var strs = ['test-123456789','test-1234-123456789','test-12345'];
var rx = /\b\d{4}(\d{5})$/;
for (var s of strs) {
var m = s.match(rx);
if (m) {
console.log(s, "=>", m[1]);
} else {
console.log("Fail for ", s);
}
}
You can try this:
var test="test-123456789";
console.log((test.match(/[^\d]\d{4}(\d{5})$/)||{1: null/*default value if not found*/})[1]);
This way supports default value for when not found any matching (look at inserted comment inline above code.).
You can use a positive lookbehind (?<= ) to assert that your group of 5 digits is preceeded by a group of 4 digits without including them in the result.
/(?<=\d{4})\d{5}$/
var inputs = [
"test-123456789", // 56789
"test-1234-123456789", // 56789
"test-12345", //fail or not giving anything
]
var rgx = /(?<=\d{4})\d{5}$/
inputs.forEach(str => {
console.log(rgx.exec(str))
})

Assistance with a regular expression Javascript

I'm trying to achieve this regular expression check. (1 integer, 3 digits)
Valid:
0.236
0.21
1.231
1.01
Invalid:
12.23
12321
0.21323
I would like to have only 1 digit follow by a decimal with only 0-3 decimal places.
Any help would be great. I have tried this:
^(([0-9]{1})?(?=\.)[0-9]{0,3})|([0-9]{1})$
but no lock.
Edit: I should have added that I'm using a JQuery plugin called inputmask. I would like for the inputmask to only accept my requirement.
To do this with jquery-inputmask, use this
<input id="example2" data-inputmask-regex="/^\d{1}\.\d{0,3}$/" />
And then in your JavaScript file, add this
$(document).ready(function(){
$("#example2").inputmask("Regex");
});
Here is the Regex breakdown
^ - Regex must start with this expression
\d{1} - Exactly 1 digit from 0 to 9
\. - Followed by a period. Important to note that periods must be escaped
\d{0,3} - Followed by 0 to 3 digits
$ - Regex must end with this expression
I tested for all of your examples.
I would like to have only 1 whole number follow by a decimal with only 0-3 decimal places.
By "1 whole number" I take it you mean one digit, as 12 is a single whole number. If so:
\d\.\d{0,3}
That matches a single digit followed by a . followed by zero to three digits. If you want to further assert that it matches the entire string, add anchors to either end:
^\d\.\d{0,3}$
Note that the rules you've given allow for 1., which seems like you may not want. If you don't, then we need to do a bit more work:
^\d(?:\.\d{1,3})?$
That says: One digit optionally followed by a . with 1-3 digits. It has the "whole string" anchors, remove them if you don't want them.
Live Example using that last one:
var input = document.querySelector("input");
var rex = [
/\d\.\d{0,3}/,
/^\d\.\d{0,3}$/,
/^\d(?:\.\d{1,3})?$/
];
input.oninput = input.onpaste = input.onkeypress = updateDisplay;
function updateDisplay() {
rex.forEach(function(r, index) {
var display = document.getElementById("r" + index);
if (!input.value) {
display.innerHTML = "--";
} else if (input.value.match(r)) {
display.innerHTML = "valid";
} else {
display.innerHTML = "INVALID";
}
});
}
<input type="text">
<p><code>/\d\.\d{0,3}/</code> says: <span id="r0"></span></p>
<p><code>/^\d\.\d{0,3}$/</code> says: <span id="r1"></span></p>
<p><code>/^\d(?:\.\d{1,3})?$/</code> says: <span id="r2"></span></p>

validate amount field using javascript

i been looking everywhere for a solution but nothing which quiet matches my criteria.
i want to enable the user to input any positive numbers with (optional 2 decimal places separated by a dot '.')
i.e: 1.23, 12.23, 123.23, 1234.23, 1, 0
how can i do this?
i attempted to use this regex
$('form').submit(function(){
var amount = $('#amount');
var regex = new RegExp("^\d+(\.\d{1,2})?$");
var error = 0;
if(amount.val() == '' || amount.val() <= 0 || regex.test(amount.val()) == false){
$('label', amount.parent()).addClass('error');
error++;
}else{
$('label', amount.parent()).removeClass('error');
}
if(error > 0){
return false;
}
});
i am not very experienced with regex, and attempted to use the code above which i found through my search but it doesn't seem to work and i don't know why. i am not sure if it is an error in the regex or the way i am using it.
Use var regex = new RegExp("^[-+]?\d+(\.\d\d?)?$"); for 2 decimal places(1st compulsory, 2nd not).
Something like:
^\d+(\.\d{1,2})?$
Note we dropped the sign part [-+]? because you stated only positive numbers.
Explanation:
^ start of string
\d+ match zero or more digits
(\.\d{0,2})? match the pattern in the brackets 0 or 1 time
\. match a literal .
\d{1,2) match 1 or 2 digits
$ end of string
So this allows some digits optionally followed by a . and 1 or 2 more digits. Adjust as needed.
var rx = /^\d+(\.\d{1,2})?$/;
alert(rx.test("1.22") + "\n" // true
+ rx.test("10") + "\n" // true
+ rx.test("2223.4") + "\n" // true
+ rx.test("foo") + "\n" // false
+ rx.test("1..22") + "\n" // false
+ rx.test(" 1")); // false
The regex you've posted allows any number of decimal places. This one only allows 2 decimal places
var regex = new RegExp("^[-+]?\d+(\.\d\d?)?$");
It's long and ugly and I don't remember where I initially got it but this is the one I use (it validates proper comma separation as well, and allows for optional $ at the beginning):
var regex = /^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(.[0-9]{2})?$/;
Here's a working fiddle: http://jsfiddle.net/5v9zcxrm/5/
For the decimal places I use (.[0-9]{2}) to only validate 2 decimal places or zero. if you want to allow one decimal place use:
var regex = /^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(.[0-9]{1,2})?$/;
If all you need is numbers (with no commas) and a possible decimal of one or two digits use:
var regex = /^\d+(.[0-9]{1,2})?$/;
$('form').submit(function(){
var amount = $('#amount');
var regex = /^\d+(\.\d{2,2})?$/;
var error = 0;
if(amount.val() == '' || amount.val() <= 0 || regex.test(amount.val()) == false){
$('label', amount.parent()).addClass('error');
error++;
}else{
$('label', amount.parent()).removeClass('error');
}
if(error > 0){
return false;
}
});
this is my updated solution which works as intended. special thanks to Matt Burland and everyone who made this possible

javascript regular expression test for 6 digit numbers only. comma seperated

and so this must pass:
454555, 939999 , 019999 ,727663
its for a user entering 6 digit invoice numbers. it should fail if a number is 5 or 7 digit and not 6. so 1234567, 123456 should fail, as one set is more than 6 numbers.
So far I have :
[0-9]{6}(\s*,*,\s*[0-9]{6})*
which only draw back is that it accepts 7 or more digit numbers. cant figure out if its even possible at this point to do both, test for 6 digits separated by a comma and one or more space, and all the digits have to be only 6 digits and fail if one is not.
any help appreciated. regular expressions are not my forte.
thanks
Norman
You can write it using regex like the function below.
const isPassword = (password: string) => /^\d{6}$/gm.test(password);
And here is an example test file below.
test('should recognize a valid password', () => {
expect(isPassword('123456')).toBe(true);
expect(isPassword('000000')).toBe(true);
});
test('should recognize an invalid password', () => {
expect(isPassword('asdasda1234')).toBe(false);
expect(isPassword('1234567')).toBe(false);
expect(isPassword('a123456a')).toBe(false);
expect(isPassword('11.11.11')).toBe(false);
expect(isPassword('aaaaaa')).toBe(false);
expect(isPassword('eeeeee')).toBe(false);
expect(isPassword('......')).toBe(false);
expect(isPassword('werwerwerwr')).toBe(false);
});
In order to validate the full string you can use this regex.
^(\s*\d{6}\s*)(,\s*\d{6}\s*)*,?\s*$
It works with six digits only, and you have to enter at least one 6 digit number.
It also works if you have a trailing comma with whitespaces.
It's accepting more than six digit numbers because you're not anchoring the text, and for some odd reason you're optionally repeating the comma. Try something like this:
^[0-9]{6}(?:\s*,\s*[0-9]{6})*$
Also note that [0-9] is equivalent to \d, so this can be rewritten more concisely as:
^\d{6}(?:\s*,\s*\d{6})*$
Your regex does not match 7 digits in a row, but it also doesn't enforce that it matches the whole string. It just has to match some substring in the string, so it would also match each of these:
"1234512345612345612345"
"NaNaNaN 123456, 123456 BOOO!"
"!##$%^&*({123456})*&^%$##!"
Just add the start of string (^) and end of string ($) anchors to enforce that the whole string matches and it will work correctly:
^[0-9]{6}(\s*,*,\s*[0-9]{6})*$
Also note that ,*, could be shortened to ,+, and if you only want one comma in a row, just use ,, not ,* or ,+.
You can also replace [0-9] with \d:
^\d{6}(\s*,\s*\d{6})*$
Using only regex:
var commaSeparatedSixDigits = /^(?:\d{6}\s*,\s*)*\d{6}$/;
if (myInput.test(commaSeparatedSixDigits)) console.log( "Is good!" );
This says:
^ - Starting at the beginning of the string
(?:…)* - Find zero or more of the following:
\d{6} - six digits
\s* - maybe some whitespace
, - a literal comma
\s* - maybe some whitespace
\d{6} - Followed by six digits
$ - Followed by the end of the string
Alternatively:
var commaSeparatedSixDigits = /^\s*\d{6}(?:\s*,\s*\d{6})*\s*$/;
I leave it as an exercise to you to decipher what's different about this.
Using JavaScript + regex:
function isOnlyCommaSeparatedSixDigitNumbers( str ){
var parts = srt.split(/\s*,\s*/);
for (var i=parts.length;i--;){
// Ensure that each part is exactly six digit characters
if (! /^\d{6}$/.test(parts[i])) return false;
}
return true;
}
I see a lot of complication here. Sounds to me like what you want is pretty simple:
/^(\d{6},)*\d{6}$/
Then we account for whitespace:
/^\s*(\d{6}\s*,\s*)*\d{6}\s*$/
But as others have noted, this is actually quite simple in JavaScript without using regex:
function check(input) {
var parts = input.split(',');
for (var i = 0, n = parts.length; i < n; i++) {
if (isNaN(+parts[i].trim())) {
return false;
}
}
return true;
}
Tested in the Chrome JavaScript console.
There isn;t any real need for a regexp. Limit the input to only 6 characters, only accept numbers and ensure that the input has 6 digits (not show here). So you would need:
HTML
<input type='text' name='invoice' size='10' maxlength='6' value='' onkeypress='evNumersOnly(event);'>
JavaScript
<script>
function evNumbersOnly( evt ) {
//--- only accepts numbers
//--- this handles incompatabilities between browsers
var theEvent = evt || window.event;
//--- this handles incompatabilities between browsers
var key = theEvent.keyCode || theEvent.which;
//--- convert key number to a letter
key = String.fromCharCode( key );
var regex = /[0-9]/; // Allowable characters 0-9.+-,
if( !regex.test(key) ) {
theEvent.returnValue = false;
//--- this prevents the character from being displayed
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>

Categories

Resources