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!

Validate for mulitple of 5

Status
Not open for further replies.

DonLo

Technical User
Apr 24, 2001
36
0
0
US
This seems like it should be simple, but I can't get it, so I'd appreciate any help.

I've got a text box in which a user enters a number, but I want to make sure that the number is a multiple of 5 (e.g., 5, 10, 15, etc.). I know I need to use a function that I can either call on an onBlur event for the textbox, or the onSubmit event for the form's submit function. I'm just not sure what that function should look like.

Can anyone help?

Thanks

Don
 
Erm, something like this maybe. Just pass it the number.
Code:
function multipleOfFive( num ) {

	if ( num >=5 ) {

		var remainder = num % 5;
	}

	if ( remainder == 0 ) {

		... whatever
	} else {
		...whatever
	}
}

It might look a bit weird to be testing
Code:
remainder == 0
when
Code:
num >=5
is false but it'll work because
Code:
remainder
will be
Code:
'undefined'
and therefore not zero.

HTH.
 
DonLo, theboyhope,

The modula operator will also help with this:
Code:
<script language=&quot;JavaScript&quot;>
function ck5(inta){
if(inta % 5 == 0 && inta != 0){
 alert(&quot;multiple&quot;); }
else {
 alert(&quot;not&quot;);
 }
}
</script>

 
theboyhope,

excuse me uhmmm..

When I read your I thought you were using division.

Nevermind, my apologies.
 
Thanks all!

theboyhope's post was what I needed!

Don
 
theboyhope's script worked, except that it doesn't check for a null value, either nothing entered in the text box, or a space. Is there some way I can
1) check for a null value or a &quot; &quot; and
2) reset the value of the textbox to 0 ?

Thanks
 

Well, you didn't ask for that. :D

Currently blank or space will be treated as not a multiple. What would you like it to do?
 
Well, I need to have a number in this text box. So, ideally, if the user deletes what's there and leaves nothing, or replaces what's there with a space, I'd like to insert the value 0.
 
I should add that I've tried to do this with the following code in the function, but it doesn't seem to reset the value in the textbox:

if ( remainder != 0 )
{
alert(&quot;You can only redeem Commuter Bonus vouchers in multiples of 5&quot;)
document.redeemFrm.AmtReqCB.focus()
document.redeemFrm.AmtReqCB.value == &quot;0&quot;
return false
}
 

Have a look at this example and tell me if it's doing what you want, please. :)

Code:
<script type=&quot;text/javascript&quot;>

function multipleOfFive( obj ) {

	var spaces = / +/;
	if ( obj.value == &quot;&quot; || spaces.test( obj.value) ) {

		obj.value = 0;
		obj.focus();
		return false;

	} else {

		if( isNaN( parseInt( obj.value ) ) || !( obj.value >= 5 && obj.value % 5 == 0 ) ) {

			alert( &quot;You can only redeem vouchers in fives.&quot; );
		}
	}
}

</script>

<input type=&quot;text&quot; onblur=&quot;multipleOfFive( this );&quot; />
 
Yes this did it, especially the first couple of lines:

var spaces = / +/;
if ( obj.value == &quot;&quot; || spaces.test( obj.value) )

I don't quite understand it, but it clearly allows me to check for not only &quot; &quot;, but also &quot; &quot; and &quot; &quot;

Thanks for your expertise!
 

It's a regular expression. The + means &quot;one or more of the previous character&quot;.
 
This was my goofy version:
Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>JavaScript Sample</title>
  </head>
  <body>
    <form name=&quot;MainForm&quot;>
      <p>Pony up a number, Skippy: <input type=&quot;text&quot; id=&quot;Numero&quot; value=&quot;0&quot; onBlur=&quot;if ((parseInt(MainForm.Numero.value)>0)&&(parseInt(MainForm.Numero.value)%5==0)&&(parseInt(MainForm.Numero.value)==MainForm.Numero.value)) {alert('Divisible by five!')} else {alert('not divisible by five (or could be equal to 0, or some bogus string, or blank).')};if(isNaN(parseInt(MainForm.Numero.value))){MainForm.Numero.value='0'+''};if(parseInt(MainForm.Numero.value)!=MainForm.Numero.value){MainForm.Numero.value='0'+''};return true;&quot;></input></p>
      <p>Have a nice day.</p>
    </form>
  </body>
</html>

I probably coded it backwards, but it seems to work on all sorts of bizarre combinations.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top