Answers.Body: <p>There are many ways you can achieve this</p>
  <ol>
  <li>Implementing <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_validateForm/" rel="noreferrer">hook_civicrm_validateForm()</a> hook via extension/module/plugin to add php code .</li>
  </ol>
  <p>code:</p>
  <pre><code>function MYMODULE_civicrm_validateForm($formName, &amp;$fields, &amp;$files, &amp;$form, &amp;$errors) {
    // 12 is the custom field id
    // inspect on form to get the exact field name
    if (!empty($fields['custom_12'])) {
      preg_match(&quot;/^[0-9]{0,6}$/&quot;, $fields['custom_12'], $matches);
      if (empty($matches)) {
        $errors['custom_12'] = ts('Error message.');
      }
    }
  }
  </code></pre>
  <ol start="2">
  <li>Implementing <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/" rel="noreferrer">hook_civicrm_buildForm()</a> hook to add js via extension/module/plugin or from theme function</li>
  </ol>
  <p>code:</p>
  <pre><code>function MYMODULE_civicrm_buildForm($formName, &amp;$form) {
    // Replace the FORM_NAME with Form name
    if ($formName == 'FORM_NAME') {
      CRM_Core_Resources::singleton()-&gt;addScript(
        &quot;CRM.$(function($) {
          // on custom field blur or change OR submit button hit
          // Check if the value in text is upto 6 digit using match()
          // display error using CRM.alert(ts('Error message'), ts('Error'));
        });&quot;
      );
    }
  }
  </code></pre>
  <ol start="3">
  <li>You can add <a href="https://www.w3schools.com/tags/att_input_pattern.asp" rel="noreferrer">HTML5 patter</a>n to do the validation. Civi provides attributes to be added into Custom field, however this is not visible through GUI, you can use mysql query or api3 or api4 explorer to add.</li>
  </ol>
  <p>Mysql code:</p>
  <pre><code>-- Add attribute for custom field id 12
  UPDATE civicrm_custom_field SET attributes = ' pattern=&quot;^[0-9]{0,6}$&quot; ' WHERE id = 12;
  </code></pre>
  <p>API3:</p>
  <p><a href="https://i.sstatic.net/CMLTG.png" rel="noreferrer"><img src="https://i.sstatic.net/CMLTG.png" alt="enter image description here" /></a></p>
  <p>API4:</p>
  <p><a href="https://i.sstatic.net/exsSv.png" rel="noreferrer"><img src="https://i.sstatic.net/exsSv.png" alt="enter image description here" /></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-22T23:16:06.677Z
Answers.Text: Wow you went all out on this one. Cool.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-22T21:30:03.537Z
Answers.LastActivityDate: 2021-12-22T21:30:03.537Z
Answers.UserDisplayName: Pradeep Nayak
Answers.Body: <p>If this was Drupal and you were happy doing this via a Webform then webform_validation might be your friend - and could potentially enforce 'unique' too if that mattered</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-23T04:31:37.270Z
Answers.LastActivityDate: 2021-12-23T04:31:37.270Z
Answers.UserDisplayName: petednz - fuzion
Answers.Body: <p>You could make the field a dropdown list or autocomplete with a million option choices (ha ha! I'm joking. Partly - it <em>would</em> technically work...)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-23T04:32:09.083Z
Answers.Text: a good answer for this crazy xmas period
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-22T23:18:19.323Z
Answers.LastActivityDate: 2021-12-22T23:18:19.323Z
Answers.UserDisplayName: Demerit
Body: <p>Have a custom field on Contact called as 'Employee ID #'. We want to restrict user/staff from submitting the form from adding more than 6 digits. How to add validation on custom field to accept up-to 6 digits only? i.e between 0 to 999999.</p>
  
Tags: <custom-fields>
Title: Validation on custom field
ThreadText: # Validation on custom field
  
  (Asked by: username_0 on 2021-12-22)
  
  Have a custom field on Contact called as 'Employee ID #'. We want to restrict user/staff from submitting the form from adding more than 6 digits. How to add validation on custom field to accept up-to 6 digits only? i.e between 0 to 999999.
  
  **Tags:** custom-fields
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2021-12-22. Score: 3
  
  There are many ways you can achieve this
  
  1. Implementing [hook\_civicrm\_validateForm()](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_validateForm/) hook via extension/module/plugin to add php code .
  
  code:
  
  ```
  function MYMODULE_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
    // 12 is the custom field id
    // inspect on form to get the exact field name
    if (!empty($fields['custom_12'])) {
      preg_match("/^[0-9]{0,6}$/", $fields['custom_12'], $matches);
      if (empty($matches)) {
        $errors['custom_12'] = ts('Error message.');
      }
    }
  }
  
  ```
  
  2. Implementing [hook\_civicrm\_buildForm()](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/) hook to add js via extension/module/plugin or from theme function
  
  code:
  
  ```
  function MYMODULE_civicrm_buildForm($formName, &$form) {
    // Replace the FORM_NAME with Form name
    if ($formName == 'FORM_NAME') {
      CRM_Core_Resources::singleton()->addScript(
        "CRM.$(function($) {
          // on custom field blur or change OR submit button hit
          // Check if the value in text is upto 6 digit using match()
          // display error using CRM.alert(ts('Error message'), ts('Error'));
        });"
      );
    }
  }
  
  ```
  
  3. You can add [HTML5 patter](https://www.w3schools.com/tags/att_input_pattern.asp)n to do the validation. Civi provides attributes to be added into Custom field, however this is not visible through GUI, you can use mysql query or api3 or api4 explorer to add.
  
  Mysql code:
  
  ```
  -- Add attribute for custom field id 12
  UPDATE civicrm_custom_field SET attributes = ' pattern="^[0-9]{0,6}$" ' WHERE id = 12;
  
  ```
  
  API3:
  
  [![enter image description here](https://i.sstatic.net/CMLTG.png)](https://i.sstatic.net/CMLTG.png)
  
  API4:
  
  [![enter image description here](https://i.sstatic.net/exsSv.png)](https://i.sstatic.net/exsSv.png)
  
  > **Comments:**
  > **username_2**: Wow you went all out on this one. Cool.  
  
  ---
  
  ### Answer by username_3 on 2021-12-23. Score: 2
  
  If this was Drupal and you were happy doing this via a Webform then webform\_validation might be your friend - and could potentially enforce 'unique' too if that mattered
  
  
  ---
  
  ### Answer by username_2 on 2021-12-22. Score: 2
  
  You could make the field a dropdown list or autocomplete with a million option choices (ha ha! I'm joking. Partly - it *would* technically work...)
  
  > **Comments:**
  > **username_3**: a good answer for this crazy xmas period  
  
  ---
  
  