The create identity form shall not allow spaces in the first name and last name fields

How can I edit the following validation script to ALSO not allow spaces when typing in the first or last name in the this particular form?

  <Field displayName="First Name" helpKey="First name of the user" name="firstname" postBack="true" required="true" type="string">
      <ValidationScript>
        <Source>
        import sailpoint.tools.Util; 
        if (Util.isNotNullOrEmpty(value)) { 
          if (!Character.isUpperCase(value.codePointAt(0))) {
             return "First Name must start with upper case letter";
          } 
        }
        return null;
       </Source>
      </ValidationScript>
    </Field>
    <Field displayName="Middle Name" name="middleName" type="string"/>
    <Field displayName="Last Name" helpKey="Last name of the user" name="lastname" postBack="true" required="true" type="string">
      <ValidationScript>
        <Source>
        import sailpoint.tools.Util; 
        if (Util.isNotNullOrEmpty(value)) { 
          if (!Character.isUpperCase(value.codePointAt(0))) {
             return "Last Name must start with upper case letter";
          } 
        }
        return null;
       </Source>
      </ValidationScript>
    </Field>

in your validation script you can add a additional if block to tackle the use case.

if(Character.isWhitespace(value.charAt(0))){
return "Cannot have empty space at the starting of first name";
}
2 Likes

I haven’t worked with Identity forms yet, but maybe a condition like this would be useful?

if(!value.matches("^[A-Z]\\S+")){
    return "First Name must start with an upper case letter and contain no spaces.";
}

It will validate if the first character is uppercase, and no space characters exist afterward. The pattern can be adjusted to match whatever other conditions might be needed.

It did not work in the validation script.

Could you please verify the below for the first name is coded correctly?

 <Field displayName="First Name" helpKey="First name of the user" name="firstname" postBack="true" required="true" type="string">
      <ValidationScript>
        <Source>
        import sailpoint.tools.Util; 
        if (Util.isNotNullOrEmpty(value)) { 
          if (!Character.isUpperCase(value.codePointAt(0))) {
             return "First Name must start with upper case letter";
          } 
        }
        return null;
          if(Character.isWhitespace(value.charAt(0))){
						return "Cannot have empty space at the starting of first name";
				}
       </Source>
      </ValidationScript>
    </Field>

Anything after “return null” will be ignored since you’re telling the script to end. Just remember the white space condition will only be true if the first character is a space.

<Field displayName="First Name" helpKey="First name of the user" name="firstname" postBack="true" required="true" type="string">
      <ValidationScript>
            <Source>
                  import sailpoint.tools.Util; 
                  if (Util.isNotNullOrEmpty(value)) { 
                        if (!Character.isUpperCase(value.codePointAt(0))) {
                              return "First Name must start with upper case letter";
                        }
                        else if(Character.isWhitespace(value.charAt(0))){
                              return "Cannot have empty space at the starting of first name";
                        }
                  }
                  return null;
             </Source>
      </ValidationScript>
</Field>
1 Like

Hi @derrickthomasvdot

If you are looking for solution to check space at the beginning of the First Name, you can check @jgruendike’s solution. If you are looking to avoid whitespace in the whole string you can use the below snippet

        import sailpoint.tools.Util;

        if (Util.isNotNullOrEmpty(value)) { 
            if (!Character.isUpperCase(value.codePointAt(0))) {
               return "First Name must start with upper case letter";
            } else if (value.contains(" ")){
                return "First Name cannot have empty space";
            }
          }
          return null;

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.