How can I check if a number has a -, +, x or nothing in front of it?
Options to check would be:
20
+20
-20
x20
20 being any number.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you wanted to be a little more flexible and allow whitespace so you were looking for the first non-whitespace character that comes before a digit, you could use a regular expression:
Working example here: http://jsfiddle.net/jfriend00/fJcQJ/
Or, if you wanted a function that would return prefix and number, you could do this:
And, a working example with test cases: http://jsfiddle.net/jfriend00/DCxcb/