• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

check only 1 string out of 3 has a value

Hi

I have the following getters getting me the value of 3 previous input form field values. Only 1 should be completed (e.g. if tpn has a value OTHER THAN "" then dln and typ SHOULD BE "")

String tpn = form.getTaxiPlateNumber();
String dln = form.getDriversLicenceNumber();
String typ = form.getType();

How can I check to see that at least 1 is completed, but NO MORE

Many thanks
Neil
0
Neil Thompson
Asked:
Neil Thompson
  • 6
  • 5
  • 3
  • +1
1 Solution
 
Gibu GeorgeCTOCommented:
You need to add the condition while setting the values to form
0
 
CEHJCommented:
Put them in an array and iterate
0
 
CEHJCommented:
It would certainly be better to do this at the UI though. You could disable / hidethe other two fields
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Gibu GeorgeCTOCommented:
yes add the validation to the UI itself
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
I'm just bodging my way in Java, can you show me some code using my example please? I'm usig struts if that makes any differnce so this is in the action form?

Thanks
Neil
0
 
CEHJCommented:
Keep the three elements in an array and put the following attribute in each input tag:

onblur="disableOtherTwo(this)"

The function called would be something like:

function disableOtherTwo(formElement) {
	for(i = 0;i < elements.length;i++) {
		elements[i].enabled = (elements[i] == formElement);
	}
}

Open in new window

0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Thanks, so I would make them an array by something like this?

arrElem[] = Array (tpn,dln,typ);
0
 
CEHJCommented:
Yes, that kind of thing
0
 
objectsCommented:
use something like this:

int completed = 0;
if (tpn.length()>0) completed++;
if (dln.length()>0) completed++;
if (typ.length()>0) completed++;
if (completed==1) {
   // only one completed
}

0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Spot on, thanks
Neil
0
 
CEHJCommented:
That won't prevent them doing the 'wrong' thing
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
This was the final bit of code from above, the action does the validation:
Thanks for all your help,
Neil
String tpn = form.getTaxiPlateNumber();
		String dln = form.getDriversLicenceNumber();
		String drn = form.getDriversName();
		int completed = 0;
 
		if (tpn.length()>0) completed++;
		if (dln.length()>0) completed++;
		if (drn.length()>0) completed++;
 
		if (completed==1) {
			// only one completed so check
			if (tpn.length()>0) {
			    //Validate Taxi Plate Number
			    errors = PayHCLValidator.validateTaxiPlateNumber(form.getTaxiPlateNumber(), PayHCLProperties.taxiPlateNumberPattern, errors);
			}
			if (dln.length()>0) {
				//Validate Drivers Licence Number
				errors = PayHCLValidator.validateDriversLicenceNumber(form.getDriversLicenceNumber(), PayHCLProperties.driversLicenceNumberPattern, errors);
			}
		} else {
			// more than one completed so return error
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("global.error.validating", "Information Fields", "Only 1 field can be completed per licence type"));
		}
 
    //Validate Type
    errors = PayHCLValidator.validateHCLType(form.getType(), errors);

Open in new window

0
 
objectsCommented:
glad I could help :)

0
 
CEHJCommented:
>>This was the final bit of code from above, the action does the validation

Yes, that will work but of course is wasteful of bandwidth and server resources when it can be done on the client
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Good point CEHJ but luckily this is internaly so not a problem but when I move this to a web based environment i will move to client side.

Regards
Neil
0
 
objectsCommented:
Its not a waste of resources, it is a necessity (and a very standard one)

:)

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now