How to validate empty Text Field?

Hi,
I am Thirupathi,
I am new to JSF. I am able to validate the value of a component which has some value using attribute
validator="#{UserNumberBean.validate}"
in this case it is calling validate method of UserNumberBean.
but if i keep the field empty then it is not calling that validate method of UserNumberBean.
Please can any one clarify my doubt.
wait for reply
Thanx in advance.
Thirupathi Reddy
mail Id: [email protected]

validate method is called to validate the correctness of the value input by user (eg. email format, phone numebr etc.). If user leaves the input box empty then there is nothing to validate and so validate is not called.
If you want customised messages for required fields you can use requiredMessage attribute in your inputText tag provide that you are using JSF1.2. It is not available in JSF1.1. In JSF1.1 it is requires some extra coding. You can do like this :
In your managed bean
public class MyBean {
    private String userName = "";
    private String password = "";
    private ArrayList errors = new ArrayList();
    public String getErrors() {
        String message;
        if (errors.size() == 0) {
            message = "";
        } else {
            message = "<FONT COLOR=RED><UL>\n";
            for(int i=0; i<errors.size(); i++) {
                message = message + "<LI>" +
                        (String)errors.get(i) + "\n";
            message = message + "</UL></FONT>\n";
        return(message);
    public String validateUser() {
        errors.clear();
        if (userName  == null || userName.trim().length() == 0) {
            errors.add("User Name is required");
        if (password == null || password.trim().length() == 0) {
            errors.add("password is required");
        if (errors.size() > 0) {
            return(null);
        } else {
           //perform your logic here
           return("returnString");
}In your jsp page, include the errors tag as below. It will show all errors on top of the page.
<f:view>
        <h:form>
            <h:outputText value="#{MyBean.errors}" escape="false"/>
            <p>User Name: <h:inputText value="#{MyBean.userName}" /></p>
            <p>Password: <h:inputText value="#{UserBean.password}" /></p>
            <h:commandButton value="Login" action="#{UserBean.validateUser}" />
        </h:form>
    </f:view>One point to remember is to clear the errors List in every action.
Hope it helps.
-Hitesh

Similar Messages

  • How to validate an text field item using javascript for numbers only.

    hi,
    how to validate an text field item using javascript for numbers only.please help me on this urgent
    please mail me solun if posible on [email protected]

    Hi,
    Page HTML header
    <script>
    function onlyNum(evt) {
      // Usage: onKeyPress="return onlyNum(event)"
      evt = (evt) ? evt : window.event;
      var charCode = (evt.which) ? evt.which : evt.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        var status = 'This field accepts numbers only!';
        alert(status);
        return false;
      var status = '';
      return true;
    </script>Item HTML Form Element Attributes
    onKeyPress="return onlyNum(event)"Br,Jari

  • How to Validate the Text Field in JSF

    Hi,
    I am facing a issue to validate the text field where the below code accepts (space' ',underscore'_',pipe'|') symbol as a first character and last character.Where by it can accept in the middle.
    It should not accept special characters also. Below code is working properly for special characters.
    <tr>
    <td width="30%" align="right"><b><h:outputText id="aAccrualType" value="Accrual Type" /> </b> <span style="color:red">*</span></td>
    <td width="50%">: <h:inputText id="accrualType" value="#{accrualTypeMast.accrualType}" required="true" >          
    <t:validateRegExpr pattern="^[(a-z)||(A-Z)||(0-9)||(_)||(\s)||(/)]+$" />     
    <f:validateLength minimum="1" maximum="50"/>     
    </h:inputText>
    <h:message for="accrualType" showDetail="true" style="color:red" />
    </td>
    </tr>
    The thing is i dont have much knowledge in regular expression.
    Is there any other way to do this.
    Could anyone help me in this.
    Thanks,
    Ambika&#9786;

    This has nothing to do with JSF.
    I suggest you to read the Pattern API documentation [1]. You can easily create a small Java Application and test the regexp using String#matches() as it uses the same regexp guidelines.
    public static void main(String[] args) {
        String regexp = "^[a-z]+$";
        String value = "foo";
        boolean match = value.matches(regexp);
        System.out.println(value + " matches " + regexp + ": " + match);
    }[1] http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

  • How to validate a text field??

    Can any one tell me hot to validate a text field??
    I want to create a text field which holds alphabets only..

    You can use simple javascript code in the liveChange function such as the following (for sap.m.Input):
    liveChange: function(evt) {
    var inputValue = evt.getSource().getValue();
    this.setValueState(me.validateTagName(inputValue));
            this.validateTagName = function(inputValue) {
        if (inputValue.trim().length < 1) {
        return "Error";
        } else {
        return "None";
    Ran

  • Empty text field warning

    I want to have a book printed from Aperture 1.5.4, but when I press the 'Buy Book' button, a dialog pops up:
    "*Empty text field detected in book*
    The book you are about to order contains an empty text field.
    How do you wish to proceed? Cancel/Continue"
    That's all very nice, to give me a warning like that, but how on earth am I going to find one invisible text field in a 50-page book full of colour photos??
    Does anyone have a hint? How problematic is it to leave the text field in? Common sense dictates that an empty text field should not be visible in the printed book either, but knowing Apple's fickleness in book matters... can I be sure? I can generate and check the PDF of course, but I don't know if that's sufficient proof.
    Thanks!

    prauz wrote:
    I want to have a book printed from Aperture 1.5.4, but when I press the 'Buy Book' button, a dialog pops up:
    "*Empty text field detected in book*
    The book you are about to order contains an empty text field.
    How do you wish to proceed? Cancel/Continue"
    That's all very nice, to give me a warning like that, but how on earth am I going to find one invisible text field in a 50-page book full of colour photos??
    hi, prauz
    sounds like there is a text box (unfilled) and you haven't typed nor pasted text into it.
    it's not that the text field is invisible, it's there yet to be selected and filled in with your information. so it is visible. have another looksie at it, look for the default letters or "Title Information Here"...
    victor

  • Error Every Single Time I Buy a Book: "Empty text field detected in book"

    I have the latest greatest Aperture and it STILL has NO WAY to tab through all text boxes so that I can find the offending "empty" text fields -- I see one other user had this issue in 2007 (without any resolution) and the problem STILL exists to this day.
    I don't know exactly how we do it but I'd sure like to find those empty text fields and skip that warning with every book purchase.
    Don't tell me about the search HUD because that apparently ignores all text boxes.
    Okay, that's it for my rant.

    I have it happen too, and, with you, think that Aperture's software developers surely can let you know, at least, what page that offending "Empty Text Field" is on. The value of it (I'm presuming here) is that they're alerting you to the possibility you may have overlooked or forgotten to put an entry in; that said, if it's empty and you can't see it, it also won't appear in the finished book so it's not really a problem that will result in a badly appearing page.

  • How to add new text field in standard report

    how to add new text field in standard report?

    Hi,
    I presume you are talking about a report display in ALV and u wish to add a column to it .
    If it is a global requirement ,as in table being used there in ALV can be modified, then you can append the table and the system should pick up the same automatically from there.
    Otherwise , you can make a Z program . Modify the catalog being used in ALV.
    Regards,
    Shweta

  • How to populate the TEXT field in MIGO

    Hi folks,
    While creating material document in MIGO or MB01, i am inserting some data in TEXT ( Field in line item level ). This text data is disappearing , once mat doc is created.
    how to populate the TEXT field in MIGO?Is there any cusomisation?it's urgent .
    Regards
    pabi

    Hello Pabitra,
    I had a similar issue in my Project.
    Please verify if the SMOD enhancement MBCF0002 (EXIT_SAPMM07M_001) is active in your system.
    If this is the case, verify from two angles:
    a) Whether the Enhancement was accidentally activated - If yes, then De-activate the same
    b) If there is some logic coded in the User Exit, but, the Text is somehow not determined...even the existing content in the text field (input to EXIT_SAPMM07M_001) will get vanished.
    Hope that your problem will be resolved by this.  Pls reward points.
    With Regards
    Vijay Gajavalli

  • How to collapse unused text field in PDF form

    Hi all, may i know how to collapse unused text field in a PDF form. Etc, we have alot of description line for user to enter the info, but some line will be left unused, so i was wondering whether can i hide those unused text field. But when i need them, i just expand it and use it.

    I don't think w/o UI Customization it is possible.
    UI customization won't be a small one as it is a common wizard and used at multiple places. So it will be a huge work.

  • How  to validate the mandatory fields in the view in WDA?

    Dear experts,
                            Iam new to WDA. i just want to know how  to validate the mandatory fields in the view in WDA?please refer any documents for validating the mandatory fields.
    Regards,
    murthy.
    Edited by: Murthy Ganti on Jul 13, 2009 2:31 PM

    Hi,
    Firstly goto view->select the element that you want to make mandatory and set the State property to required.
    Secondly place the below code in the WDDOBEFOREACTION
    DATA: l_view_controller TYPE REF TO if_wd_view_controller.
    * Get view controller
      l_view_controller = wd_this->wd_get_api( ).
    * Check for mandatory attribute
      cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
      EXPORTING view_controller  = l_view_controller
                display_messages = abap_true
    Regards,
    Radhika.

  • In Layout screen, how to validate  i/p field?

    hi,
    in Layout screen at se51, how to validate  i/p field?
    Regards,
    bb.
    Edited by: BBR on Jul 24, 2008 4:23 PM

    Hi
    if u want to validate the screen fields in Module pool programming  then u need to go for
    CHAIN & ENDCHAIN.
    Following is the example which guide u to use the chain and endchain:
    In the flow logic of ur screen before the module write this code
    CHAIN.
    FIELD TABNAME-FIELDNAME(OR SCREEN FIELD NAME).
    MODULE MODULENAME(OR FIELD NAME).
    ENDCHAIN.
    Double click on the module name and write ur validations.
    <removed_by_moderator>
    Regards
    Chandu
    Edited by: Julius Bussche on Aug 6, 2008 1:44 PM

  • I have finished a book in Aperture 3. When I press buy book a text comes up " empty text field detected in book" I have been through the book many times and can't find it. Can I over ride it at this stage the book wants to cancel

    I have finished a book in Aperture 3. When I press" buy book' a text comes up " empty text field detected in book" I have been through the book many times and can't find it. Can I over ride it?  At this stage the book wants me to cancel

    I have been through the book many times and can't find it. Can I over ride it?  At this stage the book wants me to cancel
    Sure, you can override the warning and press "Continue" instead.
    But you said "you have been through the book many times" - have you checked the pages in Aperture's book view or have you checked the preview of the book? I'd look at least at at the "Preview" of the Book - after pressing "Continue" you will see the "Preview" button when the next panel is shown. Look carefully at the preview of the book, if any text is missing.
    If you are sure that you like what you see in the preview and no empty textfields show, you can proceed to buy the book.
    Regards
    Léonie

  • How to rotate a Text Field?

    Is there anyone who knows how to rotate a Text Field? I just need to move some degrees (for example: clockwise).
    Thanks in advance for any help you can provide

    You can onlt rotate it in 90 degree imcrements ...this is controlled on the
    layout tab next to the anchor setting.
    Paul

  • How do i detect am empty text field condition?

    Hi im a newbie with swing. I have a small problem. I have a text field. When user types some text into it i display a pane in the GUI. When the text field is cleared, i need to hide the pane. So basically i need to detect the condition when the text field is empty.
    I tried one approach in which i capture key press event. So when user presses the backspace key to clear the text field, the event is fired. In the keyPress method, i check if text field is empty using getText() method.
    So my keyPressed method is as follows:
    keyPressed()
    if(textField.getText().trim().equals(""))
         // make my pane invisible here
    This does work , however it requires one additional key press before detecting that the text field is empty. For ex if there are 3 letters in the text field, i require to press the backspace key 4 times to hide the pane. I think this is because the key presses event occurs first & is handled & after that the GUI is repainted with the new data.
    Any idea how i can tackle this? Or is there any other way in which i can detect the moment the text field becomes empty?

    Demo:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class DocumentListenerExample {
        public static void main(String[] args) {
            final JLabel state = new JLabel();
            final JTextField field = new JTextField(10);
            field.getDocument().addDocumentListener(new DocumentListener(){
                public void insertUpdate(DocumentEvent e) {
                    change();
                public void removeUpdate(DocumentEvent e) {
                    change();
                public void changedUpdate(DocumentEvent e) {
                private void change() {
                    String text = field.getText().trim();
                    state.setText(text.length()==0 ? "empty field" : "non-empty field");
            JPanel cp = new JPanel(new GridLayout(1,2,8,8));
            cp.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
            cp.add(field);
            cp.add(state);
            field.setText("enter your text here");
            JFrame f = new JFrame("DocumentListenerExample");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(cp);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }

  • How to make a text field become empty when clicked in.

    I have an input textbox, if the user clicks 'submit' and the
    box is empty, it returns "please type answer here" in the input
    box. Is there a way to make it so when the user clicks in the input
    box it will turn back to blank and be ready to type instead of
    having to select the existing text and delete it?
    Thanks :)

    yep, use the onSetFocus method.
    my_text_field_instance_name.onSetFocus = function() {
    my_text_field_instance_name.text = "";
    but be careful here, because if the user fills in there info,
    then clicks off the text field then back on, the text will
    disappear, you will need to use a combo with onKillFocus or an if
    staement.... play with it. its real simple
    corey

Maybe you are looking for