[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

Validate number with cfscript

I have an input field on a form which should only accept number. How do I check if only numbers have been entered in a SCSCRIPT ?
0
Errol Farro
Asked:
Errol Farro
  • 4
1 Solution
 
adonis1976Commented:
<cfscript>
If (REFind([0-9],form.filed_name) {
return false;
}
return true;
</cfscript>
0
 
adonis1976Commented:
sorry

<cfscript>
If (REFind([0-9]),form.filed_name) {
    return false;
}
   return true;
</cfscript>
0
 
adonis1976Commented:
<cfscript>
If (REFind("[^0-9]"),form.filed_name) {
    return false;
}
   return true;
</cfscript>
0
 
adonis1976Commented:
sorry bud.. this one..

<cfscript>
If !(REFind([0-9]),form.filed_name) {
    return false;
}
   return true;
</cfscript>
0
 
Dain_AndersonCommented:
For this type of situation, you wouldn't really use CFSCRIPT, since that would be processed before the HTML output is generated or after (upon submitting the form). If you want to do client-side validation, use CFFORM and the REQUIRED attribute of the CFINPUT tag:

<cfform name="myForm" action="myPage.cfm" method="post">
<cfinput
   type = "Text" name = "MyValue"
   message = "You must enter a valid number"
   validate = "integer" required = "yes">
</cfform>

By using CFFORM, ColdFusion will insert client-side JavaScript for validation, based on what type of validation routine you're requesting.

An alternative method would be to use your own JavaScript without using CFFORM. On your Submit button, you could add something like onClick="return ValidateForm":

<form name="myForm" action="myPage.cfm" method="post">
<input
   type = "Text" name = "MyValue">
<input type="Submit" value="Submit" onClick="return ValidateForm();">
</form>

<script language="JavaScript">
function ValidateForm() {
      sErr = '';
      sErr = sErr + isValidNumber("myForm", "myValue", "\n - Please enter a valid numeric value.", "yes");
      if (sErr != ''){
            alert("******* Error *********" + sErr);
            return false;
      } else {
            return true;
      }
}

function isValidNumber(pvStrFormName, pvStrFieldName, pvStrErrMsg, bAlsoCheckNULL ,snumType)
{
      if (bAlsoCheckNULL == 'yes')
      {
            var err_chknull =  isNotNULL(pvStrFormName, pvStrFieldName, pvStrErrMsg)
            if (err_chknull.length > 0)
                  return pvStrErrMsg
      }
      
      if ( snumType == 'int' )
      {
            checkOK = "0123456789-";
      }
      else if (snumType == 'pint')
      {
            checkOK = "0123456789";
      }
      else if (snumType == 'dec')
      {
            checkOK = "0123456789-.";
      }      
      else if (snumType == 'pdec')
      {
            checkOK = "0123456789.";
      }      
      else
      {
            checkOK = "0123456789.";
      }      

        var checkStr = document.forms[pvStrFormName].elements[pvStrFieldName].value;
        var allValid = true;
        var decPoints = 0;

        var allNum = "";
        for (i = 0;  i < checkStr.length;  i++)
        {
            ch = checkStr.charAt(i);
            for (j = 0;  j < checkOK.length;  j++)
                    if (ch == checkOK.charAt(j))
                  break;
      
            if (j == checkOK.length)
            {
                    allValid = false;
                    break;
            }
            allNum += ch;
        }

        if (allValid){
            if (isNaN(Number(checkStr))){
                  allValid = false;
            }
      }
      
        if (allValid){
            if ((snumType == 'pdec') || (snumType == 'pint')){
                  if (Number(checkStr) < 0){
                        allValid = false;
                  }
            }
      }

        if (allValid){
            //MS SQL Int type limits
            var min_number='-2147483648';
            var max_number='+2147483647';
            if ( (Number(checkStr) < parseInt(min_number)) || (Number(checkStr) > parseInt(max_number)) ){
                  return pvStrErrMsg;
            }
      }
      else{
            return pvStrErrMsg;
      }

      return '';
}
</script>

HTH,

-Dain
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now