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

salemsap
Tera Expert

Better create a string field and create a on change client script to validate the data using regular expression.


I am thinking this is the way to go. I found a regular expression that would work but I'm unsure how to use that regular expression within script... how would I format that?



The reg ex is:


(19[6789]\d|20[01]\d)


Hi Tess,



To use the regex, use a line similar to the following:



var answer = /(19[6789]\d|20[01]\d)/.test(g_form.getValue('filename') +'');



The answer variable will contain true or false depending on whether or not the field value matches the regex.



Edit:   Changed "reggae" to "regex", not even sure if that was autocorrect or me.


Write onchange script use below code.



checkExpire();


function checkExpire()


{


  var res = 'ok';


  var re=/^(20|19)\d\d+$/; // This reg expression can valid only if the year start with 19 or 20


  var expiredate='19163'; //Ex: it can tale 1900,etc 2012etc but it wont allow to enter 1890and 2121




  if(!re.test(expiredate))


  {


          alert('Please enter valid year');


                  }


              else if(expiredate.length >4 || expiredate.length<4)


            {


                  alert('Please enter only 4 digits year');


            }


else


{


alert('perfect');


}


}




Regards,


Harish.