JRS: In article <MP************************@news.pa.comcast.gigane ws.co
m>, seen in news:comp.lang.javascript, Dave Miller <un*****@unknown.com>
posted at Fri, 17 Oct 2003 14:39:52 :-
I have a form field the results from which should not start with a
digit. I have tried various permutations of the following without
success. Can anyone help me out?
//does not return false when it should
(document.my_form.my_field.value.substring(0) == / \d /)
The field.value is a string. There are exactly three types of string :
Starting with a digit
Starting with a non-digit
Not starting - i.e. = "" // do not forget this case
To test string S :
OK = !/^\d/.test(S) // does not start with digit
OK = /^\D/.test(S) // starts with non-digit
It seems likely that in practice you have other conditions on the entry;
the first-character test should be combined with the others using a
longer RegExp, e.g.
OK = /^\D.{3,5}$/.test(S) // non-digit; length 4..6
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.