Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating a mask for field input

Guest
Apr 14, 2011 Apr 14, 2011

I have a cfinput that requires the serial number for a piece of equipment.  The number is always something like J123M456789 where the J or the M could be any letter and the 123 and 456789 could be any number that is 3/6 characters.  The code i am using for my input is as follows:

<cfinput type="text" name="IDnumber" message="You must enter an 11 character meter number" mask="A999A999999" validateat="onSubmit" validate="maxlength" required="yes" class="SERIAL"id="IDnumber" maxlength="11" width="240" minlength="11">

I am still able to enter any number, any which way i choose. 

Can anyone help?

1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 14, 2011 Apr 14, 2011

If you are attempting to use mask="" to do client-side validation, Coldfusion handles it using Javascript.  (Edit: You can verify this by doing View Source on your page).  So, your question boils down to your Javascript not working.  This may be because of two problems:

1) When testing, you have Javascript disabled.  Obviously, the answer here is to enable it.

2) You have other Javascript on your page which is breaking.  I usually get Firefox browser and Firebug plugin to find out if this is the case, and to help debug.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 14, 2011 Apr 14, 2011

Javascript is working on the page and I have no other javascript running.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 14, 2011 Apr 14, 2011

Woops, my bad

Some of the validation is working.  It is forcing me to input a letter, 3 numbers, a letter and then numbers again.  The trouble is it will let me submit at any time.  In otherwords if I place 11 characters in the text field, they must be in the proper format however i cant get it to force me to enter exactly 11 characters.

HELP!!!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2011 Apr 14, 2011

The MASK attribute just controls what can be typed into the field, it doesn't perform any validation, per se (there's a subtle difference).  However when I try using a mask, it seems OK.

What you want to do for validation is to use a regex, as Dan rather obscurely suggested.

And, as was also mentioned: client-side validation is a "nice to have" but the actual validation must be done server side.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 14, 2011 Apr 14, 2011

I am still able to enter any number, any which way i choose.

According to the documentation, that is the expected behavior:

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f51.html

"...the mask attribute controls the format of data that can be entered into a text field ... In HTML format, it does not prevent users from typing a that does not follow the mask. You can combine masking and validation on a field."

You could combine the mask with a regex validation, or implement custom handling onKeyUp. (Not that you should rely on javascript validation alone).

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 14, 2011 Apr 14, 2011

As cfSearching mentioned, you should have server-side code to validate that the data is correct. However, I would encourage you to look at jQuery and jQuery Validate plugin to accomplish what you want. You could then do something like:

$.validator.addMethod("correctCodeFormat", function(value, element) {

return isInt(value.substring(0,2)) && isChar(value.substring(3,3) ...

}

$('#myForm').validate(

}

});

Easy, no?

--

Matthew Widiger

Web Developer

Scitent Technologies

"Resources For Online Education"

400 Preston Avenue • Suite 250

Charlottesville, VA 22903

Direct: 434.220.5730

Main: 434.244.5060 • Fax: 434.244.5059

www.scitent.com

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2011 Apr 14, 2011

Easy, no?

Easier than putting a pattern attribute on a CFINPUT? No.

Personally I'd use vanilla forms (as opposed to CFFORMs and CFINPUTs) with

JQuery validation, but this is not an example of where JQuery is easier

than CFML.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 15, 2011 Apr 15, 2011

Ah, but if we are discussion real-time client-side submit validations, your choice is Javascript or...Javascript.  jQuery is easier than writinglg custom JS, is all I'm saying.  CFML doesn't do realtime submit validation (although it would be cool if it did!)  Apples vs. Oranges.  I agree with you though - not a big fan of CF* for forms, I also use vanilla and jQuery.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 15, 2011 Apr 15, 2011
LATEST

mw_scitent wrote:

CFML doesn't do realtime submit validation (although it would be cool if it did!)

Well then enjoy the chill.  ColdFusion DOES do some JavaScript "real-time" submit validation.

I.E. Some of the tags will auto-generate the relevant JavaScript to do the requested task.  Such as form field validation.

As with all wizards, it can be useful for lots of basic tasks, but it is not hard to go beyond the abilities of the wizard generated code.  And then one would need to tap more sophisticated code, with which JQuery might very well help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2011 Apr 14, 2011

Regular Expressions are your friends, in a user unfriendly sort of way.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources