how about a reg ex
/^\d+(\.25|\.5|\.50|\.75)?
Main Topics
Browse All TopicsI have a Input Box that a user inputs a whole or decimal number..
i want to validate that a user can only: -
input a whole number eg.. 10, 20 etc..
OR
if a number is specified after the decimal point it can only be a quarter,half, three quaters eg.. (.25, .5 or .50,.75)
Whats the easiest way to validate this ??
Cheers
JT
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If you want to do it in Coldfusion, the code is:
<cfif FORM.currency*100 MOD 25 NEQ 0>
Invalid Currency
<cfelse>
Good Currency
</cfif>
I recommend though that you also do it in javascript like below:
Put this in your header....
<script type="text/javascript">
<!--
function validate(form){
if ( (form.currency.value*100)%
alert('Currency must be 25 cent increments.');
return false;
} else {
return false;
}
}
-->
</script>
And do your form something like this :
<form name="yourForm" action="index.cfm" method="post" onSubmit="return validate(this)">
<input type="text" name="currency">
<input type="submit">
</form>
Sorry, in the javascript that second "return false;" is supposed to read "return true;"
I forgot to change it back after testing.
New Code:
<script type="text/javascript">
<!--
function validate(form){
if ( (form.currency.value*100)%
alert('Currency must be 25 cent increments.');
return false;
} else {
return true;
}
}
-->
</script>
looping is inefficient
here is a js test
if(!/^\d+(\.25|\.5|\.50|\.
alert("Currency is invalid")
}
and here is CF
<cfif not REFindNoCase("/^\d+(\.25|\
error code goes here
</cfif>
one line check for all instances
If you wanted a coldfusion regular expression, the correct way to do it is:
<cfif NOT REFindNoCase("^\d+(\.|\.0|
Coldfusion doesn't use the "/" that javascript does
I still think the MOD route is better though so you dont have to go through all the "OR"s... but either work
this should work for ya...
<script type="text/javascript">
<!--
function validate(obj){
if (!obj.Currency.value.match
alert('Currency must be 25 cent increments.');
return false;
} else {
return true;
}
}
-->
</script>
<form name="test" action="" method="post" onSubmit="return validate(this);">
<input type="text" name="Currency">
<input type="submit" name="submit" value="Submit">
</form>
you could also do something like this...
<script type="text/javascript">
<!--
function validate(obj){
var curr=obj.Currency.value.sp
if(!curr[1].match(/^(50|5|
alert(curr[1]);
return false;
}else{
return true;
}
}
-->
</script>
<form name="test" action="" method="post" onSubmit="return validate(this);">
<input type="text" name="Currency">
<input type="submit" name="submit" value="Submit">
</form>
and this way you can have values like this... 1,234.50 but not... 1,234.51 or 1,234.556
Thanks trailblazzyr55
When you get the time can you have a look at the following question
http://www.experts-exchang
Remember i was trying to work out the UK tax weeks, i have stumbled across a javascript function that does it but i am trying to make sense of it and convert it into a cfscript !! and add into my DateInfo.cfc which you kindly helped me out with !!
jturkington, did my javascript work for you? also have a look at the other post you mentioned...
http://www.experts-exchang
let me know if you need any help =)
regards,
~trail
Business Accounts
Answer for Membership
by: mkishlinePosted on 2006-02-01 at 07:27:28ID: 15843934
If you are willing to do it server-side, something like this will work
<cfset valid = true />
<cfset validdecimals = "25,5,50,75" />
<cfloop index="i" list="#form.number#">
<cfif NOT isNumeric(i)><cfset valid = false /><cfbreak /></cfif>
<cfif i EQ ListLast(form.number)>
<cfif ListFind(validdecimals,i ) EQ 0>
<cfset valid = false /><cfbreak />
</cfif>
</cfif>
</cfloop>
<cfif NOT valid>
Your number was not a valid decimal
</cfif>