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!

Numeric Validation

Status
Not open for further replies.

gk4u

Programmer
Jul 2, 2002
21
IN
Hi All,
I am new to ColdFusion.I have a Input box as text field.
I want this field should accept numeric values only.
For example : 1,2,300,90,0 etc.
It should accept NULL ot Blank also.

Thanks in advance.
Gaurav
 
if you are using CFFORM, you can use
Code:
<cfinput 
   type = "Text" name = "FieldName" 
   message = "You must enter an integer." 
   validate = "integer" required = "No">


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
NO I am not using CFFORM.
Please suggest me other method.

Gaurav
 
in the form handler (which I usualy make the same page)

<cfset errorMsg = "">
<cfif isDefined("Submit")>
<cfif not isNumeric("form.numberField")>
<cfset errorMsg = "This is not a number.<br>">
<cfif errorMsg = "">
<cfquery datasource = "dsn">
insert into table (field) values (<cfif trim(form.numberField) eq "">
NULL
<cfelse>
#form.numberField#
</cfif>)
</cfquery>
</cfif>



Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
grr... did it again.
<cfif errorMsg = "">
should read
<cfif errorMsg eq "">


Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
WELL ALRIGHTY THEN...

you can use bombboy's suggestion as a CF solution, or you can use javascript for client side validation
Code:
function IsNumeric(sText) {
   var ValidChars = "0123456789 ";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
   
}


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top