Expand my Community achievements bar.

Validate month number as text

Avatar

Level 2

I am trying to validate a text field so that it only accepts the month in numerical form with a leading zero if less than 10. The data type is Text Field, the value is  “User Entered - Required” and the Validation pattern is 99.

I have the following code:

----- topmostSubform.Page1.DateMM::exit: - (JavaScript, client) --------------------

if (this.isNull) {

    xfa.host.messageBox("Field cannot be blank!");

}

else {

    if (xfa.event.commitKey == 2){

        if (this.rawValue >= "01" && this.rawValue <= "12") {

            xfa.host.setFocus("DateDD");

        }

        else {

            this.rawValue = "";

            xfa.host.messageBox("Please enter month number between 01 and 12");

        }

    }

}

----- topmostSubform.Page1.DateMM::validate: - (JavaScript, client) ----------------

if (this.rawValue >= "01" && this.rawValue <= "12")

{

    true;

}

else

{

   

    this.rawValue = "";

    false;

}

----- topmostSubform.Page1.DateMM::change: - (JavaScript, client) ------------------

if (xfa.event.commitKey == 2){

        if (this.rawValue >= "01" && this.rawValue <= "12") {

            xfa.host.setFocus("DateDD");

        }

        else {

            this.rawValue = "";

            xfa.host.messageBox("Please enter month number between 01 and 12");

        }

}

Since the field is required, I want the message “Field cannot be blank!” to be displayed if when entered on the field and nothing is entered. If validation doesn’t pass (month from 01 to 12), delete the text and display error message. As it is now The validation seems to work, but the error messages are not showing except the “field cannot be blank”. If I enter incorrect text I get the “Field cannot be blank” twice.

3 Replies

Avatar

Former Community Member

Put this code on the exit event of the field. If the user enters a single digit then a 0 will be added.. Make sure the field is a numeric firld and it is set for an integer. This will make sure the user cannot enter a letter.

if (this.rawValue.length < 2){

this.rawValue = "0" + this.rawValue

}

if ((this.rawValue > 12) || (this.rawValue < 1)){

app.alert("The field must conatain a value from 01 - 12")

this.rawValue = ""

xfa.host.setFocus(this.name)

}

Paul

Avatar

Level 2

This works fine, except that I am not getting the app.alert mesage at anytime, except my "Empty Message" and only after I entered something in the field.

Avatar

Former Community Member

If you enter 0 or something greater than 12 the message should appear.I created a field with a comb of 2 chars so that the user coudl not enter more than that.

Paul