2

I use jsf form with a few inputs. Some inputs are required. I check on an empty space through required="true". But i need to check before submit form if user just typed a few gaps space-key from keyboard, cause then the value is stored in the database, but I don't need it. I thought it may be implement using jQuery method trim(), but I'm a new to jQuery and I have no idea how to implement it.

<h:form prependId="false">
    <div class="row">
        <div class="col-lg-3 form-group">
            <label>First Name<span class="text-danger"> *</span></label>
            <h:inputText value="#{userController.user.firstName}" 
                styleClass="form-control" id="firstName"
                required="true" requiredMessage="#{bundle.common_error_empty_value}"/>
            <h:message for="firstName" styleClass="validationMsg"></h:message>
        </div>
        <div class="col-lg-5 form-group">
            <label>Last Name:<span class="text-danger"> *</span></label>
            <h:inputText value="#{userController.user.lastName}" 
                styleClass="form-control" id="lastName"
                required="true" requiredMessage="#{bundle.common_error_empty_value}"/>
            <h:message for="lastName" styleClass="validationMsg"></h:message>
        </div>
    </div>
   //etc...
</h:form>

I tried something like this. Maybe someone have an idea how I can check fields on white spaces.

<script type="text/javascript">
            function trimInputs() {
                $('form input').each(function () {
                    $.trim(value);
                });
            }
        </script>
Jeroen
  • 1,168
  • 1
  • 12
  • 24
user3127896
  • 6,323
  • 15
  • 39
  • 65
  • You need just to trim it and save in the DB? What happens if the user types only blank-spaces, are you going to allow him to save the record? – Aritz Jan 22 '14 at 12:12
  • When the user types only blank-spaces I need to show him error validation message like I've shown if user just leave filed blank and pressed submit form. – user3127896 Jan 22 '14 at 12:25
  • 1
    Maybe question's title is misleading. You don't want the value to be trimmed, you want it not to be saved if empty or if only has blank spaces (btw this isn't what you actually are trying to do with jQuery). I encourage you to change the tile for better description of the problem. – Aritz Jan 22 '14 at 12:39
  • For those who want to silently trim anyway, head to this answer: https://stackoverflow.com/a/33241290 – BalusC Jan 26 '16 at 13:18

3 Answers3

7

If you use Hibernate validators then it could go with @mabi's solution, which is also explained here. Otherwise, you could also write your own JSF validator:

@FacesValidator
public class NoBlankSpaceValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
        //Check if user has typed only blank spaces
        if(value.toString().trim().isEmpty()){
            FacesMessage msg = 
                new FacesMessage("Incorrect input provided", 
                        "The input must provide some meaningful character");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);

        }
    }
}

And use it as:

<h:inputText value="#{userController.user.firstName}" 
    required="true" requiredMessage="#{bundle.common_error_empty_value}"
    validator="noBlankSpaceValidator"/>

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
4

You could do this the old fashioned way:

<h:inputText id="lastName"
onchange="this.value = this.value.trim()"
/>

And add whatever validations you want. The onchange fires before the validations do, so it will act as if the user did not enter any spaces at the beginning or end of the input.

Dovev Hefetz
  • 1,346
  • 14
  • 21
  • 1
    Wouldn't that fire on each letter I type, effectively preventing me from entering any spaces? – Robert Nov 15 '16 at 23:04
  • @Robert - no, the onchange will only fire after you finish changing the field. onkeydown/up/press would fire on each letter you type. – Dovev Hefetz Nov 22 '16 at 07:56
0

From your "save to the DB" bit, maybe adding a @NotBlank constraint to the corresponding field is all that you need.

See this this answer for an example.

Community
  • 1
  • 1
mabi
  • 5,279
  • 2
  • 43
  • 78
  • I think it's a good solution for Hibernate, but not for JSF framework. – user3127896 Jan 22 '14 at 12:31
  • Yeah, if you don't have a validation framework around, @Xtreme Biker's answer is what you need. Note that Hibernate Validator ties into JSF's validation phase so it's useful even if you don't use Hibernate as an ORM. – mabi Jan 22 '14 at 12:44