The CreatorCon Call for Content is officially open! Get started here.

How do you create a field that holds only a year eg. 1997?

therese_erickso
Kilo Explorer

Would it be best to create a String field and somehow put restrictions on that? If so, what type of restrictions would I apply?

Or is there a way to use the existing Date field to do this?

1 ACCEPTED SOLUTION

var reg = /(19[6789]\d|20[01]\d)/ ;


var ans = g_form.getValue('fieldname');


if(!reg.test(ans))


{


        alert('Please enter valid year');


}


View solution in original post

9 REPLIES 9

var reg = /(19[6789]\d|20[01]\d)/ ;


var ans = g_form.getValue('fieldname');


if(!reg.test(ans))


{


        alert('Please enter valid year');


}


DINESH CHINNAD1
Giga Expert

I guess some validations should be used in client script to do this...


tltoulson
Kilo Sage

Hi Tess,



There are a couple of options depending on your specific requirements.   I'll summarize here and if you need help with a specific one, let me know and I'll try to help out further.



  • Choice List:
    • Advantage- No data validation is needed, works like a string field in scripts
    • Disadvantage- Could be difficult if there is not an upper/lower limit on year choices or a temporary artificial one that can be created
  • String:
    • Advantage- The year 3000 is easy to accommodate without a really long choice list or maintenance of the list
    • Disadvantage- Requires a minimum of a client script or ui policy to ensure the year is a number and potentially other validation
  • Integer:
    • Advantage- Letters already can not be entered into an integer field so no need to validate that, scalable like the string field
    • Disadvantage- May still require other client scripts / ui policies for validation (does the year -43625 sound reasonable/valid?).   Also includes a "," or "." separator that I am not certain if it can be removed on a field by field basis (I never tried before).


I hope this helps.



Kind regards,



Travis


Anurag Tripathi
Mega Patron
Mega Patron

Hi Tess,



I think easises and least effort will be this way



Use a normal date time field and write an onchange script, on the script read the year selected and if its anything except 1997 then clear the field and throw an alert.



You can make it more generic by using a property and reading the year value from the property(comma separated)


-Anurag

therese_erickso
Kilo Explorer

Thanks everyone for your input! This is now working; thanks again.