10 examples of 'javascript validation for numbers only' in JavaScript

Every line of 'javascript validation for numbers only' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
809static validateNumber (val) {
810 return Number(val);
811}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

135function validateNum(value){
136 if(/^\d{4,11}$/.test(value)){
137 return true;
138 }else{
139 return false;
140 }
141}
8function checkNumber({ def, value }) {
9 const expectsInteger = def.integer;
10
11 // Is it a valid number?
12 if (typeof value !== 'number' || Number.isNaN(value)) {
13 return { type: ErrorTypes.EXPECTED_TYPE, dataType: expectsInteger ? 'Integer' : 'Number' };
14 }
15
16 // Assuming we are not incrementing, is the value less than the maximum value?
17 if (def.max !== undefined && (def.exclusiveMax ? def.max <= value : def.max < value)) {
18 return { type: def.exclusiveMax ? ErrorTypes.MAX_NUMBER_EXCLUSIVE : ErrorTypes.MAX_NUMBER, max: def.max };
19 }
20
21 // Assuming we are not incrementing, is the value more than the minimum value?
22 if (def.min !== undefined && (def.exclusiveMin ? def.min >= value : def.min > value)) {
23 return { type: def.exclusiveMin ? ErrorTypes.MIN_NUMBER_EXCLUSIVE : ErrorTypes.MIN_NUMBER, min: def.min };
24 }
25
26 // Is it an integer if we expect an integer?
27 if (expectsInteger && !Number.isInteger(value)) {
28 return { type: ErrorTypes.MUST_BE_INTEGER };
29 }
30}
62function isNumber(value) {
63 var patrn = /^(-)?\d+(\.\d+)?$/;
64 if (patrn.exec(value) == null || value == "") {
65 return false
66 } else {
67 return true
68 }
69}
35function isValidNumber(value) {
36 const regex = new RegExp('^[-]?[0-9]*$');
37 const result = regex.test(value);
38
39 return result;
40}
92var validNumeric = function validNumeric(number) {
93 var numbers = /^[0-9۰-۹٠-٩]+$/;
94 if (number.toString().replace(/,/g, '').match(numbers)) {
95 return true;
96 } else {
97 return false;
98 }
99};
12export function assertNumbers(...numbers: number[]) {
13 if (DEBUG_CHECK_NUMBERS) {
14 for (let i = 0; i < numbers.length; i++) {
15 if ("number" !== typeof numbers[i]) {
16 throw new Error(
17 `assertNumbers arguments[${i}] is not a number. ${typeof numbers[i]} == typeof ${numbers[i]}`);
18 }
19 }
20 }
21 return true;
22}
686function validateNumber(input, nullValue, decimals){
687 var text = input.value;
688 var validChars = "0123456789";
689
690 if (!nullValue)
691if (!validateNonEmpty(input))
692 return false;
693
694 if (decimals) validChars += '.';
695
696 var isNumber = true;
697 var c;
698
699 for (i = 0; i < text.length && isNumber; i++) {
700 c = text.charAt(i);
701 if (c == '-' || c == "+") {
702 if (i < 0) isNumber = false;
703 } else if (validChars.indexOf(c) == -1) {
704 isNumber = false;
705 }
706 }
707 if (!isNumber) {
708 Ext.get(input).addClass('error');
709 return false;
710 } else {
711 Ext.get(input).removeClass('error');
712 return true;
713 }
714}
40export function isNumber(num) {
41 return typeof num === 'number' && !Number.isNaN(num);
42}
32function positiveNumberValidator(control: FormControl): any {
33 if (!control.value) return null;
34 const price = parseInt(control.value);
35 return price === null || typeof price === 'number' && price > 0
36 ? null : {positivenumber: true};
37}

Related snippets