473,728 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Haitashi
96 New Member
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
9 10675
acoder
16,027 Recognized Expert Moderator MVP
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
Haitashi
96 New Member
That's it! Thanks! ^_^
Oct 10 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
No problem. You're welcome.
Oct 10 '07 #4
confusedfusion
15 New Member
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 Recognized Expert Moderator MVP
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
confusedfusion
15 New Member
@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 Recognized Expert Moderator MVP
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
confusedfusion
15 New Member
<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?
Mar 17 '09 #9
acoder
16,027 Recognized Expert Moderator MVP
@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

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

Similar topics

4
2111
by: Hernán Castelo | last post by:
hi should i validate cookies values? thanks -- atte, Hernán Castelo SGA - UTN - FRBA
4
3488
by: fred | last post by:
I'm trying to validate data entered into a form field by using a Query. I've discovered that the validation rule is pretty useless if you need anything other than very basic validation. I've tried using the following After Update Code: Dim strSQL strSQL = "SELECT tblProducts.ProductSRP FROM tblProducts" strSQL = & "Where (((tblProducts.ProductID)=!!.!));
9
3135
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to be inserted into a standard web address in the table (the filed name is link) in ddw1 Example address ---
4
3308
theaybaras
by: theaybaras | last post by:
Hi everyone, You've all been such a huge help to me since joining, and I'd just like to take a second to let you know how much I appreciate it! That said, I have another supplication! ;) I have a db of scientific article citations and data extracted from the papers. When I first made this db I knew nothing of normalization, and that has been a MAJOR pain, as you can well imagine. I have worked to get this normalized and I have just one...
1
1874
by: anilareddy | last post by:
I have an application like this. I need to validate the start time in 2nd row against the start row in first row. I mean The start time value entered in the second row must not b the value entered in 1st and the value entered in 3rd must not b the value entered in 1st,2nd etc.. like this. Please tell me how it can be done. I'm sending the code here. Its a html file. Just copy paste the code and run to execute it and see how it works. ...
5
2570
nathj
by: nathj | last post by:
Hi, I have been looking around the forum and the web for a way to achieve this and so far I have drawn a blank. So I head to the forum as I'm sure someone knows how to do this. I have a form which I validate as the user enters data, this is straight forward enough. Part of the validation ensures that all mandatory fields are entered before the continue button is enabled. (unfortunately this is a development site at present and so I can't...
1
4005
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. Depends on the pull down list selection, I use script.aculo.us to validate the user input before submit and pass the necessary data, such as contact type, contact information and ranking to a php program for updating. This form should allow multiple...
1
2836
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i am working with is using .asp. Their are 30 multiple choice questions. Each will have have 3 or 4 checkboxes where the test taker will choose only 1 answer per question. Anybody have any ideas as to the best way to validate that each question has...
4
2699
by: somuchbetta | last post by:
Hiya, Does anyone know any simple JScript code to validate a numeric field and also the length of the data that should be entered into the field. e.g. 12345 - the user can only enter numeric fields and the lenght of the value should be five (no more and no less) Thanks
0
8758
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9197
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9129
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8126
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6716
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3236
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.