0

Validating longbox

asked 2011-03-22 07:52:45 +0800

pradeep28 gravatar image pradeep28
33

hi all,
intbox,longbox,doublebox,decimalbox all are accepting %[percent symbol] and ,[comma symbol] by keyboard.
we want numeric value only. How to validate it we have constraint property in all the respective component but it is not working.
please give me some suggestion to solve the issue.

following are the code.

<longbox id="partnerTelephone" width="40%" name="partnerTelephone" value="@{partner.telephone}" maxlength="12" constraint="no negative,no zero, no empty:${c:l('common.message')} ${c:l('partner.telephone')}" readonly="${readonly}"/>

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2011-03-22 08:14:40 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

as suggestion, have a look on the attached custom constraint.

/**
 * Constraint for comparing the value from a <b>textbox</b>.<br>
 * Throws an error message if NOT empty or NOT only numbers ( 0-9 ). Used in the
 * PayConditions Searchbox for checking that the SearchToken is only integers or
 * empty</br> </br>
 * 
 * <pre>
 * Java call:</br>
 * 		Textbox searchField = new Textbox();
 * 		searchField.setConstraint(new OnlyEmptyAndIntegerConstraint());
 * </pre>
 * 
 * @author Stephan Gerth
 */
public class OnlyEmptyAndIntegerConstraint implements Constraint, java.io.Serializable {

	private static final long serialVersionUID = 4052163775381888061L;

	public OnlyEmptyAndIntegerConstraint() {
		super();
	}

	@Override
	public void validate(Component comp, Object value) throws WrongValueException {

		if (comp instanceof Textbox) {

			// no need for checking ?
			if (((Textbox) comp).isDisabled()) {
				return;
			}

			// cast the value to String and trim it.
			final String enteredValue = ((String) value).trim();

			// check if not empty
			if (!enteredValue.isEmpty()) {
				// check if not allowed signs
				if (!enteredValue.matches("(([0-9]+)?)+")) {
					throw new WrongValueException(comp, Labels.getLabel("message.error.OnlyNumbersOrEmptyAllowed"));
				}
			}

		}
	}
}

// Check for numbers
// --------------------
// if ("-2324.00".matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
// System.out.println("Is a number");
// } else {
// System.out.println("Is not a number");
// }


best
Stephan

link publish delete flag offensive edit

answered 2011-03-23 06:29:34 +0800

pradeep28 gravatar image pradeep28
33

Thanks For your response Mr.Stephan
That class works fine.

and please help me to validate ip address [i.e it should only contain number and period like this (192.168.1.2)]
please help me in validating ip address.

regards
Pradeep.M

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-03-22 07:52:45 +0800

Seen: 640 times

Last updated: Mar 23 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More