Problem: I'm trying to validate when the user only inputted special characters without typing any number with it.
I'm using this expression
/^\+?[0-9 \.-]+$/
to accept only '+' sign, numbers, dots, hypens, and spaces when validating fax. This is working fine.
But with that expression the user can input --------------
without typing any number and is accepted because it contains hypen.
Question: Is there's a way to check if the input contains number? not just all special characters?
UPDATE:
This is an example of valid accepted input.
+1-2 12-98765.43
> the requirement is it should only accept '+' sign, hypen, numbers, spaces and dots.
Problem: I'm trying to validate when the user only inputted special characters without typing any number with it.
I'm using this expression
/^\+?[0-9 \.-]+$/
to accept only '+' sign, numbers, dots, hypens, and spaces when validating fax. This is working fine.
But with that expression the user can input --------------
without typing any number and is accepted because it contains hypen.
Question: Is there's a way to check if the input contains number? not just all special characters?
UPDATE:
This is an example of valid accepted input.
+1-2 12-98765.43
> the requirement is it should only accept '+' sign, hypen, numbers, spaces and dots.
- 1 Can you add all possible valid input strings. i.e. fax numbers – Tushar Commented Feb 19, 2016 at 4:18
- I added a sample output. That's their only requirement for the output. – bot Commented Feb 19, 2016 at 4:39
-
Check
^\+?\d-\d\s\d{2}-\d{5}\.\d{2}$
– Tushar Commented Feb 19, 2016 at 5:13
3 Answers
Reset to default 4Probably the easiest option is to have additional regex checks for each condition. E.g. have a regex check for just the presence of numbers /[0-9]/
and another check for just the presence of special characters /[ +.-]/
. Run these only after testing that nothing undesirable exists in the string.
var whole = /^\+?[0-9 \.-]+$/
function validate(input) {
// input only contains valid things
if (!input.test(whole)) { return "Input must contain only numbers, spaces, and + . or -"; }
// input contains each required thing
if (!input.test(/[0-9]/)) { return "Number required"; }
if (!input.test(/[ .-]/)) { return "Special character required"; }
// You can also test the first character of the string with charAt()
if (input.charAt(0) !== "+") { return "Input does not begin with +"; }
return "Valid input";
}
I notice that your regex tests for zero or one plus, followed by a character in the list [numbers, spaces, periods, or hyphens]. Do you mean to test for any number of pluses? The regex I've posted (/[ +.-]/
) should work for all the characters you want to allow.
I'm not sure this is what you're looking for, but if you want to verify that a specific single character or pattern exists in a string, you can use indexOf:
// Require at least one hyphen
if (input.indexOf("-") === -1) { return "Please include a hyphen"; }
Update: If, as in your example, there is only one plus and it is at the beginning, then you do indeed want the \+?
bit. However, you don't need to escape the period inside of square brackets. Supposing the plus were required, you could use charAt to test this. See updated example.
Just add a lookahead
.
^(?=.*[0-9])\+?[0-9 \.-]+$
See demo.
https://regex101./r/eB8xU8/9
This is an example of valid accepted input.
+1-2 12-98765.43 > the requirement is it should only accept '+' sign, hypen, numbers, spaces
Accepted input appear to accept .
character as well ?
Try ^(\+\d-\d \d{2}-\d+)(?:\.\d+|$)
<form>
<input type="text" pattern="^(\+\d-\d \d{2}-\d+)(?:\.\d+|$)" required />
<input type="submit" />
</form>