469,366 Members | 2,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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

Haitashi
Expand|Select|Wrap|Line Numbers
  1.          <cfif (Attributes.icq NEQ "") AND IsNumeric (Attributes.icq) AND IsValid("integer", Attributes.icq)> 
  2.             <cfset Variables.oStudentProfile.icq = Attributes.icq />
  3.         <cfelse>    
  4.             <cfset temp = Request.oClientError.setError("icq", "Your ICQ must consist of numbers only.") />
  5.         </cfif>
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 ^_^
Oct 9 '07 #1

✓ answered by acoder

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:
Expand|Select|Wrap|Line Numbers
  1. <cfif (Attributes.icq NEQ "")>
  2.   <cfif IsNumeric (Attributes.icq) AND IsValid("integer", Attributes.icq)>
  3.     <cfset Variables.oStudentProfile.icq = Attributes.icq />
  4.   <cfelse>   
  5.     <cfset temp = Request.oClientError.setError("icq", "Your ICQ must consist of numbers only.") />
  6.   </cfif>
  7. </cfif>

9 10211
acoder
16,027 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. <cfif (Attributes.icq NEQ "")>
  2.   <cfif IsNumeric (Attributes.icq) AND IsValid("integer", Attributes.icq)>
  3.     <cfset Variables.oStudentProfile.icq = Attributes.icq />
  4.   <cfelse>   
  5.     <cfset temp = Request.oClientError.setError("icq", "Your ICQ must consist of numbers only.") />
  6.   </cfif>
  7. </cfif>
Oct 10 '07 #2
That's it! Thanks! ^_^
Oct 10 '07 #3
acoder
16,027 Expert Mod 8TB
No problem. You're welcome.
Oct 10 '07 #4
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.

Expand|Select|Wrap|Line Numbers
  1. <META HTTP-EQUIV="Refresh" 
  2. CONTENT="5; URL=http://xxx/index.php">  
  3.  
  4.  
  5. <cfif IsDefined('form.send_email')> 
  6.     <cfset errorMsg = ''> 
  7.  
  8.     <cfif Len(Trim(form.name)) EQ 0> 
  9.         <cfset errorMsg = errorMsg & '- Please enter your name<br/>'> 
  10.     </cfif> 
  11.     <cfif Len(Trim(form.email)) EQ 0> 
  12.         <cfset errorMsg = errorMsg & '- Please enter your email address<br/>'> 
  13.     <cfelseif form.email DOES NOT CONTAIN '@' OR form.email DOES NOT CONTAIN '.'> 
  14.         <cfset errorMsg = errorMsg & '- Please enter a valid email address<br/>'> 
  15.     </cfif> 
  16.     <cfif Len(Trim(form.comments)) EQ 0> 
  17.         <cfset errorMsg = errorMsg & '- Please enter your comments or questions<br/>'> 
  18.     </cfif> 
  19. <cfif Len(Trim(form.phone)) EQ 0> 
  20.         <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'> 
  21.     </cfif> 
  22. <cfif Len(Trim(form.phone2)) EQ 0> 
  23.         <cfset errorMsg = errorMsg & '- Please enter the first three numbers of your phone number<br/>'> 
  24.     </cfif> 
  25. <cfif Len(Trim(form.phone3)) EQ 0> 
  26.         <cfset errorMsg = errorMsg & '- Please enter the last four numbers of your phone number<br/>'> 
  27.     </cfif> 
  28.  
  29.  
  30.     <cfif Len(Trim(errorMsg)) GT 0> 
  31.         <cfset urlString = URLEncodedFormat(errorMsg)> 
  32.         <cfset valueString = "name=#URLEncodedFormat(form.name)#&company=#URLEncodedFormat(form.company)#&email=#URLEncodedFormat(form.email)#&phone=#URLEncodedFormat(form.phone)#&comments=#URLEncodedFormat(form.comments)#"> 
  33.         <cflocation url="#cgi.HTTP_REFERER#?email_error=1&error_msg=#urlString#&#valueString#"> 
  34.     <cfelse> 
  35.         <cfmail to="#form.to_email#" from="#form.email#" subject="#form.subject#"> 
  36. An email has been submitted to learn more from the following person: 
  37.  
  38. Name: #form.name#         
  39. Company: #form.company# 
  40. Email: #form.email# 
  41. Phone: #form.phone# #form.phone2# #form.phone3# 
  42.  
  43. Comments: #form.comments# 
  44.         </cfmail> 
  45.         <cflocation url="http://xxx/ThankYou.htm"> 
  46.     </cfif> 
  47. <cfelse> 
  48.     <p style="font:bold 12px Verdana, Arial, Helvetica, sans-serif; color:red;">You do not have access to view this page.</p> 
  49. </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
Mar 16 '09 #5
acoder
16,027 Expert Mod 8TB
Use the len() function to check for maximum length (just requires a slight modification):
Expand|Select|Wrap|Line Numbers
  1. <cfif Len(Trim(form.phone)) NEQ 3> 
  2.         <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'> 
  3. </cfif> 
To check if it's numeric, use the IsNumeric() function.
Mar 17 '09 #6
@acoder

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?
Mar 17 '09 #7
acoder
16,027 Expert Mod 8TB
Similar:
Expand|Select|Wrap|Line Numbers
  1. <cfif Len(Trim(form.phone)) NEQ 3 or not IsNumeric(Trim(form.phone))> 
  2.         <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'> 
  3. </cfif> 
Mar 17 '09 #8
<cfif Len(Trim(form.phone)) 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?
Mar 17 '09 #9
acoder
16,027 Expert Mod 8TB
@confusedfusion
Yes, you need the trim, otherwise if there's a space, it will not be counted as numeric when it could easily be ignored.
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.
Mar 17 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Hernán Castelo | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.