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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cant tell why this wont work, please help

Status
Not open for further replies.

mochalova

MIS
Sep 20, 2007
15
US
Could you please tell me what I am missing, I am trying to prevent more than 5 numbers at a time in my textarea

This is what I have but it wont work:

<script>
var testresults
function checkcomm(){
var str=document.form.COMMENTS.value
var filter=[0-9]:\{5\}
if (filter.test(str))
testresults=true
else{
alert("Please input a comment.")
testresults=false;
}
return (testresults)
}
</script>
 
I'm guessing it's on this line:
Code:
var filter=[0-9]:\{5\}

I've never seen a regexp written that way before, so unless it's something I'm unaware of, it's incorrect.

Here's an example of a regex that will check for no more than 5 numbers in a text area:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function checkcomm(obj) {
   var str = obj.value;
   if (/^(\D*\d\D*){0,5}\D*$/.test(str)) {
      alert("Validation passed");
      return true;
   }
   else {
      alert("More than 5 numbers in textarea")
      return false;
   }
}

</script>

<style type="text/css">
</style>

</head>
<body>

<textarea id="ta"></textarea>
<input type="button" value="check textarea" onclick="checkcomm(document.getElementById('ta'))" />

</body>
</html>

You might want to try indenting your code, it's something every programming student learns in CS101 for a reason. Debugging unindented code is a nightmare.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
[tt]<script>
[red]//[/red]var testresults
function checkcomm(){
[blue]var testresults[/blue]
var str=document.form.COMMENTS.value //you name the form "form"? try not to do it
[red]//[/red]var filter=[0-9]:\{5\}
[blue]var filter=/[0-9]{5,}/[/blue]
if ([highlight]![/highlight]filter.test(str))
testresults=true //pass the test: no 5+ consecutive numbers
else{
alert("Please input a comment.")
testresults=false;
}
return (testresults)
}
</script>[/tt]
 
just a note on my post above, you can get rid of the part highlighted, it's not needed:
Code:
if (/^(\D*\d\D*){0,5}[!][s]\D*[/s][/!]$/.test(str)) {

tsuji, I don't think the OP was looking for 5 consecutive numbers - I think they were looking for 5 total numbers, but I could be wrong.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
If strictly more than 5, sure it is this.
[tt] var filter=/[0-9]{6,}/[/tt]
 
Big, huge thanks to you both.
It's five consecutive numbers.

Thank you Kaht

Thank you Tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top