Expand my Community achievements bar.

Force required field

Avatar

Former Community Member

I have been attempting to force a required field using javascript. I ahve the following entered in the exit event of a text field.

(Required_Field is the name of the field )

if (this.length < 3)
{

    xfa.host.messageBox("Value Must be greater than 3 characters in length: " + Required_Field.rawValue + " number: " + Required_Field.length);
    xfa.host.setFocus(this);
}
else
{
    xfa.host.messageBox("That was a valid entry");
   
}

No matter how many characters i place in the text box, I always hit the length < 3 and the amount of that lenght as shown in the message box is always 0.

How can i force a required field? Can anyone see what i am doing wrong ?

6 Replies

Avatar

Former Community Member

Your issue is the test (this.length). "this" is the current object and as such it has no length....it is an object. What you want is the length of the rawValue property. So it shodul be:

If (this.rawValue.length <3)

Paul

Avatar

Former Community Member

Thank you, Paul. That was helpful and solved my syntax error.

In the greater scheme of things, i woul like to force a user to put information into a field, otherwise the form will not submit.

I tried putting code into the submit button enter event to make sure that the length of a specified field's raw value was greater than x, but this javascript doesn't appear to work.

How can i make a required field on a form, where the orm will not submit if that field is not filled in? I tried using a field value type of "User Entered -- Required," but this didn't work to require the field, either.

Thanks for any input.

Mike

Avatar

Former Community Member

I would like to make clear that i have no prior experience with Live Designer, so regarding my questinos on required fields and field validation, if there is an easy way to do this, i would be glad to hear of it. I don't have to use a scripting method, i just want to be able to perform validation (such as is date, or >2 < 10,etc as well as to force a field to be filled in before a form can submit.)

I think these are pretty standard requests for a form, so would be suprised if there weren't options for doing this with the Live Designer.

Thank you for any input.

Mike

Avatar

Former Community Member

To ensure that the user enters data into a field before submission you simply have to make the field required. If you want to validate the input then some simple validations are provided (validate patterns) but in most cases simple script will need to be written to verify the input. In the validation script message area, enter a message that you want to display when the validation fails. You can use the validate event to write your script as the validate event will fire before submission will occur. Then you can return a true or false value from the script to indicate whether the validation passed or failed. Write your script like this:

if

(this.rawValue < 10){

     true

}

else {

     false

}

The "this" indicates the current object that the script is running under.

Avatar

Former Community Member

I thought that making a field required should work, but obviouslty I am not doing something right.

I have added a text field to the form, and under the Object Pallette Value tab, I selected Type: User Entered -- Required. I did not put anything in the default box, but put a small message in the Empty Message box. All other values on the Value tab are left blank, excpet for Validation Scrpt Message, which has the Error checkbox checked by default.

On the form, i have a button, which is set to control type Execute. When the button is pressed, it calls a webservice.

I am able to click the button, without putting any values into the field i marked as User Entered -- Required, and the form calls the webservice. This is not what i want, i want the form to say "You must use this required field." and to not submit to the webservice unless the required field has a value.

Thank you for any input.

Avatar

Former Community Member

The required field logic is fired when you do a submit operation. The calling of a web service is not considered a submit operation hence the required field logic is not checked. If you were to email or HTTP post the form to a server then that woudl be considered a submit operation. Many Customers use web services to aid in filling out the form (getting information to populate dropdowns, get additional info once I identify my employee ID just to name a few). Validating the inputs at Web Service call time would cause these services to fail. So you will have to build in the validation yourself. You can use the required field as you have defined it but you will have to cuase the validate event to fire before you do your web service call. You can call the validate event by using this command:

FieldName.execValidate()

It will return a true or fasle value based on whether the validation succeeded or not. When you make the field mandatory that means that if there is a value in the field it will return true and if it is empty it will return false. So you can test the validation and execute the web service accordingly. Here is an example of a test that I did (Javascript):

if

(uid_string.execValidate()){

    app.alert("Executing web service...");

     invokeBtn.execEvent("click");

}

else {

     app.alert("Returned false")

}

Hope that helps