Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cfinput regular expression validation not working

Posted on 2009-04-10
11
Medium Priority
?
1,146 Views
Last Modified: 2012-05-06
I can't get regular expressions to reject anything.  I noticed invalid times getting into the database.  I even created code that said the pattern was beer and typed in time in 00:00 format and it was not rejected.

using cfinput in coldfusion on linux running debian
<cfinput type="text" name="NOTIFIED" size="5" maxlength="5" value="#TimeFormat(Notified,"HH:mm")#" validate="regex" pattern="beer" message= "wrong">

Open in new window

0
Comment
Question by:storeytime
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 16

Expert Comment

by:duncancumming
ID: 24121135
validate="regex" uses Javascript.  Does your browser have Javascript enabled?  Of course any Javascript validation can be got round, so you'd also have to do server-side validation too.

Also regex validation only works on HTML forms, not on Flash forms.

You could change the type from regex to time, although I think that expects format of hh:mm:ss.

Alternatively instead of doing a regex, you could use the mask attribute, something like mask="99:99".


0
 
LVL 2

Author Comment

by:storeytime
ID: 24121253
yes but the problem is i have people using incorrect times like hour 24:35 it doesn't exist.  so it causes database issues.  tried with firefox, Chrome, IE7
0
 
LVL 16

Expert Comment

by:duncancumming
ID: 24121275
right, so that's a different problem from originally reported.  If you want to check the time is actually valid, use
IsValid("time", form.fieldname)

0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 2

Author Comment

by:storeytime
ID: 24121287
ok the problem is i can't get regular expression to validate anything

Im ean I ty pe in ^$ and I can type anything I want into the balnk and it takes it
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24123127
storeytime,

I assume you are using format="html".  In which case using validate="time" would work fine for your purposes - because you are using maxlength="5".  As long as javascript is enabled, the attached works correctly.   BUT, users can easily disable javascript. So in addition, you should also use server side validation on the action page, as Ducan suggests.

Also, with date and times you should use cfqueryparam or CreateDateTime to ensure the values are properly formatted before being sent to the database.






<cfform format="html">
	<cfinput type="text" name="NOTIFIED" size="5" maxlength="5" 
			validate="time" message= "wrong time">
	<cfinput type="submit" name="submitButton">
</cfform>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 24123139
> ok the problem is i can't get regular expression to validate anything

Probably because the validate value should be "regular_expression", not "regex"
<cfform format="html">
	<cfinput type="text" name="onlyNumbers" required="yes" validate="regular_expression"
			pattern="[0-5]" 
			message="Only numbers 1 through 5 are allowed!">
	
	<cfinput type="submit" name="submitButton">
</cfform>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 24123158
> Probably because the validate value should be "regular_expression", not "regex"

Oops. My mistake.  They changed it to allow both "regex" and "regular_expression".  If the above code does not work for you, then you should check whether _any_ cfform validation works.  For example, if a simple required="true" does not work:

<cfinput type="text" name="firstName" required="yes" message="First Name is required!">

... either javascript is disabled -or- there is a problem with the CFIDE directory/mapping (where the validation scripts are located).  

0
 
LVL 2

Author Comment

by:storeytime
ID: 24127579
if you force onserver then the user readable message is not displayed using the message=" yada" parameter.  is there a way to display a user readable message or are you stuck with try formatting ^/A/d/d/d/d?/d$ for the message
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24129998
Whatever message you define is displayed if you use validate="time".  Apparently, it is not displayed for validate="reg_ex".  
0
 
LVL 2

Author Comment

by:storeytime
ID: 24139024
you can use validateat="when to validate" to set one or multiple times to validate i know you get a little pop up window with javascript execution of the message statement but when javascript is disabled and you also force onserver then you do not get the message you get format not correct must be and then gives regular expression which to the end user is very cryptic
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 24139325
Yes, but it IS displayed when you use onserver  <cfinput validate="time" ...> on CF8.  Which should work fine for you.   Like I said above, for whatever reason the message is is _not_ displayed when you use onserver/validate="reg_ex".  I don't believe there is a way to change that.   So if you want a more readable error message, I believe you'll have to use validate="time" , or do the validation yourself with IsValid(...).  Those are pretty much all of your options for cfform.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question