how to allow the field should accept only numbers ...
# suitescript
v
how to allow the field should accept only numbers and another field should accept only alphabets? Please anyone can help? .test function does not work well.
b
primary choice is going to be regular expressions for this one
v
what does it mean regular expression?
b
i dont believe you gave test a real try if you dont know what a regular expression is
v
you mean this? var numbers = /^[0-9]/;
b
that is a regular expression, though thats not gonna give you only numeric characters
thats first character is numeric
v
how to check all the characters?
I have 4 characters and all those four should be numeric
this is the requirement.
b
r
@vennila ramasamy I've never actually sat down and worked on regex, so I generally don't use it. Regex is an extremely technical thing that you're not going to figure out in a day. Consider iterating over the characters to inspect the string.
e
^ I agree with this. Regular Expressions are extremely valuable to add to your programming skills, but there is a steep learning curve. You can use the Number constructor to see if all the characters in a string are numeric. If all the characters are not numeric,
Number()
will return
NaN
, which you can check with
isNaN()
.
Copy code
Number("12345") // => returns 12345 as a number
Number("ABCDEF") // => returns NaN
Number("123ABC") // => returns NaN
b
not really good enough for his task,
Copy code
Number('12e3')
the other approach to the question is usually a variation of checking each character in a loop using something like charCodeAt
e
Oh good catch on exponential notation; too bad
parseInt()
does behaves differently