Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking fields against a pattern

Status
Not open for further replies.

Markh51

Programmer
May 20, 2003
81
GB
Why is it when I check my fields against this pattern, if nothing is entered then it is OK ??

^[0-9]{0,7}$

How do I make it not allow blank entrys ???

Cheers,
Mark
 
*********** SCRAP THE ABOVE QUESTION ****************

I got it sorted out.

I now need to now how I can stop people entering a number starting with 0, using th above pattern.

Cheers,
mark
 

<html>
<head>
<script language= &quot;JavaScript&quot;>
function ckdec() {
var refstring = /^0/; //sets ref for char to be searched for
var mystrg = document.form1.testval.value;
if (mystrg.search(refstring) > -1) { // if search returns found value > -1
alert (&quot;text sarts with a 0!&quot;); }
else { alert (&quot;text doesn't start with a 0!&quot;);}
}
</script>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; name=&quot;testval&quot; size=&quot;15&quot; value=&quot;enter a num&quot;>
<input type=&quot;button&quot; value=&quot;click to test&quot; onClick=&quot;ckdec();&quot;>
</form>
</body
</html>

Hope it helps.
 
Thanks for that, but all I need to know is how do I stop them entering the 0, using this pattern:

^[0-9]{0,7}$

Can you just add you bit into the pattern ?

Thanks,
Mark
 
It OK, I think I worked it out !

Or at least ^[1-9][0-9]{&quot;+lmin+&quot;,&quot;+lmax+&quot;}$ seems to work

Mark
 
/^[0-9]{0,7}$/

is not a good ref for your application

it is saying start at beg. of string &quot;^&quot;
look for any number char.'s &quot;[0-9}&quot;
that occur 0 to 7 times &quot;{0,7}&quot;

that is why I would suggest &quot;/^0/&quot;


 
I don't understand where I should put your &quot;/^0/&quot;.

Can you please modify my pattern to include yours.

Thanks,
Mark
 
Hi Mark,

It is very confusing. Working with regular expressions.

This tutorial is very helpful it has about 3 Chapters:


Looks to me like your last: &quot;^[1-9][0-9&quot;
Since you left &quot;0&quot; out of the range by using &quot;[1-9]&quot;
should work O.K.>>>but by my tests using variables
such as &quot;+lmin&quot;,&quot;+lmax+&quot; will have to be replaced with
literals such as {2,9}
 
Another thing to consider is that your leaving an opening
with your first [1-9] statement--there is no limit to the
amount of times digits can occur....

You might consider /^[1-9]{1}[0-9]{1,6}$/ this statement
limits your first occurence range to 1 then your second
occurence range is &quot;1 not more than 6&quot; for a total of
7 digits.

What I meant to say earlier was that using those variables
inside the &quot;{&quot; &quot;}&quot; didn't work for me at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top