set field max length , but in form layout , it still can input a number of data

zhihong
Tera Contributor

hi experts,

I set "Date" max length is 4, but in form layout it can input 15 ,why?

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

That's not possible. You can't restrict the user to add only 4 numbers. You could use the workaround Anurag and I mentioned, giving an error, maybe even do g_form.clearValue('u_date') to empty the field.

If really restricting the input, you can look at DOM manipulation. Though that's BAD PRACTICE. So not adviced.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

38 REPLIES 38

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Max length is mostly for string fields. If you want a restriction on integer you can count the number of in a client script and throw error.

-Anurag

-Anurag

hi Anurag,

could you show me how to achieve it ?

Here is a sample script you can use in an onchange client script on the integer field

var x = g_form.getValue('<your integer field name>');   //
if(x.toString().length >4) {
//do something
}
else{
//do something
}

Please mark my answer correct/helpful if if helps you solve your issue.

-Anurag

-Anurag

function onChange() {
 
 var date = g_form.getValue('u_date'); 
 if(date.toString().length >4) {
  g_form.addErrorMessage('only input 4 bits');
  
 } else {

 }
}

this can pop up an error message and still can input data when its length>4  , but I want make it  not edit when it length>4, how to achieve?

That's not possible. You can't restrict the user to add only 4 numbers. You could use the workaround Anurag and I mentioned, giving an error, maybe even do g_form.clearValue('u_date') to empty the field.

If really restricting the input, you can look at DOM manipulation. Though that's BAD PRACTICE. So not adviced.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn