Price Field Javascript and Jquery validation [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have to validate a price textbox field with below conditions using javascript/jquery :
Please help me out to create a jquery for below validations :
The 'Price' field will take in a negative value.
'Price' field should only display to the second decimal (and only allow a user to enter to the second decimal).
'Price' field should not be allowed to accept alphabetic characters.
'Price' field will accept multiple decimals. This value can be saved. (Multiple decimal is allowed,Regular Decimal not allowed after each decimal there can be only 2 digits).
Thanks a Ton.

You can also use regular expression to validate the value of price
^-?(\d{1,3})(\.\d{1,2})?$
This regex will accept negative as well as positive values with numbers upto 1 to 999 and also with 2 decimal places
You modify the regex as per your requirement.
\d{1,3} - will accept upto 3 digits
\.\d{1,2} - will accept decimal digits upto 2 places
EDIT
This regex will allow 999.99.99.99 or 999.99.99 type values
^-?(\d{1,3})(\.\d{1,2})*?$
Explanation
(\.\d{1,2})* - This will accept dot multiple times

Hi Please try this regular expression,
/^\d+(?:(\,||\.)\d\d?)*?$/
eg:
var newVal = $("#priceTextBox").val();
var regexp = /^(0|[1-9]+[0-9]*)$/;
if (regexp.test(newVal)) {
alert("valid Price");
}else{
alert("Invalid");
}

Related

Regular Expressions Vue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 21 hours ago.
The community is reviewing whether to reopen this question as of 10 hours ago.
Improve this question
I'm trying to validate an input with regEx in Vue, which I don't have any idea how to make one and couldn't find online how to match what I want to do.
The thing is I'm trying to validate a price that should be a float with 2 decimal numbers, and it can be 1 number before the . or 9 digits. For example:
0.50
1.00
99999.99
999999999.00
I tried this:
v => (/\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})/.test(v))
But doesn't work.
Sorry if my english is not very good. I appreciate the help!
To match 1-9 digits before the dot, and 2 decimal numbers:
^\d{1,9}\.\d{1,2}$
See a regex101 demo.
What do you want? Check the value for matching a number from 0 to 999999999 in the integer part and no more than 2 numbers after "."?
A template assuming that the entire string being checked from the beginning (^) to the end ($) consists of
mandatory initial part, which is either 0 or contains from 1 to 9 digits, and does not start with "0" ;
optional ending of "." and two digits:
^([1-9]\d{0,8}|0)(.\d{1,2})?$

Regex for this format [40]*100+ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
The community is reviewing whether to reopen this question as of 6 months ago.
Improve this question
I am struggling with a Regex for this format [40]*100+ where the user can enter up to 3 digits inside the square brackets and up to 3 next to the multiplication sign. The plus sign can only appear if the previous format of [40]*100 is respected (the plus is optional at the end). And if they extend the input they should be able to extend that format like this [40]*100+[20]*100+[40]*100+ and so on.
This determines if the input is valid but trying to control the format escapes me.
function isValid2(str) {
return !/[~`!##$%\^a-zA-Z&()=\-\\\';,/{}\\":<>\?]/g.test(str);
}
$('input.cut_text').on('input change keydown', function () {
if (isValid2(this.value) == false){
this.value = this.value.replace(/[^1-9]/g,'');
return false
}
});
This regexp tests for the format you describe:
/^(\[\d{1,3}\]\*\d{1,3}\+)+$/
\d{1,3} matches up to 3 digits. We put one of these inside literal [], with literal * after that, and literal + after the second one. Then we use a quantified group to allow multiple repetitions.
You can't do the validation until the user has finished entering the field, because it won't match the partial value while they're typing. So you can use it in a change event listener, but not input or keydown.

Convert 17digit number string to a integer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert a 17digit number string into a number
this is the number "76561197962169398".I tried using parseInt()
The result of using parseInt is :-
76561197962169390
I am loosing the last digit.I also tried BigInt() 'n' is getting appended to the number.
I m thinking of using replace() with a regex for only digits.
Is there any other way I can achieve this without loosing precision.
Please any help regarding this is really appriciated.THANK YOU
in chrome 83 devtools:
x=76561197962169398n
76561197962169398n
++x
76561197962169399n
typeof x
"bigint"
y=BigInt("76561197962169398")
76561197962169398n
++y
76561197962169399n
x+y
153122395924338798n
x + 1
VM342:1 Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
at <anonymous>:1:2
(anonymous) # VM342:1
x + 1n
76561197962169400n
[5n, 3n, 9n, 7n].sort()
[3n, 5n, 7n, 9n]
The n suffix is for display - and in code it's needed to say a literal value needs to be treated as bigint instead of number - think of it like quotes for strings - without quotes a sequence of characters is not a string - similarly a number without n suffix is not a bigint - it's a number that has limited precision and simply cannot be used for large values

how to get all number from beginning of string until first non-number? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How to get all number from beginning of string until first non-number?
For example, I want to get 12345 from '12345abc' and another example get 5678 from '5678kkk'.
Is any way can do this?
You could use RegExp#match with ^ anchor, to find out the numeric characters from beginning:
const string = "12345abc";
const matches = string.match(/^\d+/);
// Fallback if no matches found
const numbers = (matches || [])[0];
console.log(numbers);
Use parseInt() as it will stripe out all the characters other than the numeric character so you do not need custom logic for getting the numeric value as you have described:
console.log(parseInt('12345abc'));
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
Use parseInt
let str = '5678kkk';
console.log(parseInt(str));

Phone Number Validation with Regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a web form and want to accept the following phone numbers in the following format:
1234567890
123-456-7890
123.456.7890
123-4567890
The first number cannot be a 0 or a 1.
How can I do this with regex/javascript? I found a few regex formulas online but none are specific to my needs
null !== thenumber.match(/^[2-9][0-9]{2}[.-]?[0-9]{3}[.-]?[0-9]{4}$/);
(Edited to give slightly better answer with boolean result)
Consider the following Regex...
[\d-[01]]\d{2}[-\.]?\d{3}[-\.]?\d{4}
Note: You examples start with a 1 which will not satisfy the above regex.
The user-provided format should be irrelevant. It makes more sense to store phone numbers as, well, numbers, i.e. digits only, and add uniform formatting when displaying them back. Otherwise you end up wit a mess in your database and you're going to have a hard time searching for numbers (if you wanted to find if given number is already in your DB then how'd you know the format it was typed in?), and you will have inconsistent formatting of numbers when displaying them.
So you'd store any of your example numbers as 1234567890, no matter what the user has typed into the form. Which means you can validate your input by stripping any non-digits, then checking the length and other conditions, like this:
function validPhone( num ){
var digits = num.replace(/\D/g,'');
// assuming 10 digits is a rule you want to enforce and the first digit shouldn't be 0 or 1
return (parseInt(digits[0],10) > 1 && digits.length == 10);
}
You could also use return parseInt(digits, 10) >= 2000000000 to validate that the number doesn't start with 0 nor 1 and has at least 10 digits.

Categories