How can I validate that data entered is numeric and ignore if field is blank?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Haitashi
    New Member
    • Jun 2007
    • 96

    How can I validate that data entered is numeric and ignore if field is blank?

    [CODE=cfm] <cfif (Attributes.icq NEQ "") AND IsNumeric (Attributes.icq ) AND IsValid("intege r", Attributes.icq) >
    <cfset Variables.oStud entProfile.icq = Attributes.icq />
    <cfelse>
    <cfset temp = Request.oClient Error.setError( "icq", "Your ICQ must consist of numbers only.") />
    </cfif>[/CODE]

    Ok, so if the student enters something in this field I want to save that info. If the field is blank then ignore it.

    The code above give me the following problem. If it's left blank then it does the SET ERROR request.

    So, at the moment users would have to at least enter "0" in order to save.

    Also, I know ICQ is ancient... What can I say? I don't design the thing, I just code it ^_^
    Last edited by acoder; Oct 10 '07, 12:00 PM. Reason: Added language =cfm to code tag
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The problem with your code is that you're checking the data is not empty, is numeric and is a valid integer, then you have a cfelse which means if the field is not any of those things, give an error message.

    What you should do is just make a check for the field being blank separately. If non-blank, then make a check for numeric validation. If that fails, then set an error.

    That would be something like:
    [CODE=cfm]<cfif (Attributes.icq NEQ "")>
    <cfif IsNumeric (Attributes.icq ) AND IsValid("intege r", Attributes.icq) >
    <cfset Variables.oStud entProfile.icq = Attributes.icq />
    <cfelse>
    <cfset temp = Request.oClient Error.setError( "icq", "Your ICQ must consist of numbers only.") />
    </cfif>
    </cfif>[/CODE]

    Comment

    • Haitashi
      New Member
      • Jun 2007
      • 96

      #3
      That's it! Thanks! ^_^

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No problem. You're welcome.

        Comment

        • confusedfusion
          New Member
          • Mar 2009
          • 15

          #5
          anothers issue, I am working from a code I didn't create but now have to modify; The end result of my problem is to make sure that only three numbers are entered for area code, three for phone prefix and four numbers in the last field.

          Having hard time, never used CF before and need syntax to validate three fields (form.phone, form.phone2, form.phone.3) to be numeric and contain the correct number of numbers.

          Code:
          <META HTTP-EQUIV="Refresh" 
          CONTENT="5; URL=http://xxx/index.php">  
            
            
          <cfif IsDefined('form.send_email')> 
              <cfset errorMsg = ''> 
            
              <cfif Len(Trim(form.name)) EQ 0> 
                  <cfset errorMsg = errorMsg & '- Please enter your name<br/>'> 
              </cfif> 
              <cfif Len(Trim(form.email)) EQ 0> 
                  <cfset errorMsg = errorMsg & '- Please enter your email address<br/>'> 
              <cfelseif form.email DOES NOT CONTAIN '@' OR form.email DOES NOT CONTAIN '.'> 
                  <cfset errorMsg = errorMsg & '- Please enter a valid email address<br/>'> 
              </cfif> 
              <cfif Len(Trim(form.comments)) EQ 0> 
                  <cfset errorMsg = errorMsg & '- Please enter your comments or questions<br/>'> 
              </cfif> 
          <cfif Len(Trim(form.phone)) EQ 0> 
                  <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'> 
              </cfif> 
          <cfif Len(Trim(form.phone2)) EQ 0> 
                  <cfset errorMsg = errorMsg & '- Please enter the first three numbers of your phone number<br/>'> 
              </cfif> 
          <cfif Len(Trim(form.phone3)) EQ 0> 
                  <cfset errorMsg = errorMsg & '- Please enter the last four numbers of your phone number<br/>'> 
              </cfif> 
            
            
              <cfif Len(Trim(errorMsg)) GT 0> 
                  <cfset urlString = URLEncodedFormat(errorMsg)> 
                  <cfset valueString = "name=#URLEncodedFormat(form.name)#&company=#URLEncodedFormat(form.company)#&email=#URLEncodedFormat(form.email)#&phone=#URLEncodedFormat(form.phone)#&comments=#URLEncodedFormat(form.comments)#"> 
                  <cflocation url="#cgi.HTTP_REFERER#?email_error=1&error_msg=#urlString#&#valueString#"> 
              <cfelse> 
                  <cfmail to="#form.to_email#" from="#form.email#" subject="#form.subject#"> 
          An email has been submitted to learn more from the following person: 
            
          Name: #form.name#         
          Company: #form.company# 
          Email: #form.email# 
          Phone: #form.phone# #form.phone2# #form.phone3# 
            
          Comments: #form.comments# 
                  </cfmail> 
                  <cflocation url="http://xxx/ThankYou.htm"> 
              </cfif> 
          <cfelse> 
              <p style="font:bold 12px Verdana, Arial, Helvetica, sans-serif; color:red;">You do not have access to view this page.</p> 
          </cfif>
          A bounus would be to know whow to get the errors to display in a pop up; like I said this is someones code before I was hired and I only new basic HTML until this job; but when it validates and there is an error the error message is in the address bar???

          Thank you,

          CF

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use the len() function to check for maximum length (just requires a slight modification):
            Code:
            <cfif Len(Trim(form.phone)) NEQ 3> 
                    <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'> 
            </cfif>
            To check if it's numeric, use the IsNumeric() function.

            Comment

            • confusedfusion
              New Member
              • Mar 2009
              • 15

              #7
              Originally posted by acoder
              Use the len() function to check for maximum length (just requires a slight modification):
              Code:
              <cfif Len(Trim(form.phone)) NEQ 3> 
                      <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'> 
              </cfif>
              To check if it's numeric, use the IsNumeric() function.

              Sorry, my screen name says it all, OK, I see and understand "not equal to greater than 3" and IsNumeric is easy to read;

              What I want to make sure is that the length is 3 and is numeric else error;

              I forced the form to only allow three chars so a user can not add more, but do not want them to add none, less than 3 or alpha.

              What would that code look like?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Similar:
                Code:
                <cfif Len(Trim(form.phone)) NEQ 3 or not IsNumeric(Trim(form.phone))> 
                        <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'> 
                </cfif>

                Comment

                • confusedfusion
                  New Member
                  • Mar 2009
                  • 15

                  #9
                  <cfif Len(Trim(form.p hone)) NEQ 3 or not IsNumeric(Trim( form.phone))>
                  <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'>
                  </cfif>

                  on the 2nd part "or not" IsNumeric(Trim( form.phone))> would IsNumeric(form. phone)> work, do I need the "trim?"

                  I posted another reply you are looking at with this, would the cfoutput replace the cfset for the error here?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by confusedfusion
                    on the 2nd part "or not" IsNumeric(Trim( form.phone))> would IsNumeric(form. phone)> work, do I need the "trim?"
                    Yes, you need the trim, otherwise if there's a space, it will not be counted as numeric when it could easily be ignored.
                    Originally posted by confusedfusion
                    I posted another reply you are looking at with this, would the cfoutput replace the cfset for the error here?
                    No, not here. It would be at the end when all the error messages have been acummulated.

                    Comment

                    Working...