Skip to main content
Known Participant
April 6, 2023
Answered

Using java to search for a data range

  • April 6, 2023
  • 2 replies
  • 2500 views

Hi,

 

I have a two text boxes, one is the users input for a serial number and the other text box checks what was writtin as a serial number and changes what is written in it depending on the range. the serial numbers are like this:

AHB15000 - AHB15999

BHB15000 - BHB15999

CHB15000 - CHB15000

 

as there is a serial number break at KHB15103 everything before should say one thing and everything after should say something else. Other than writing the following code im not sure what to do:

 

else if (/KHB15000/.test(s)) event.value = "option 2";

else if (/KHB15001/.test(s)) event.value = "option 2";

else if (/KHB15002/.test(s)) event.value = "option 2";

else if (/KHB15003/.test(s)) event.value = "option 2";

etc.

 

Sorry im quite new to this so dont have a huge amount of knowledge!

 

this is the code I am useing:

var s = this.getField("Serial No").valueAsString; // insert actual field name

if (/KHB15//.test(s)) event.value = "Option 1";

else if (/MHB15/.test(s)) event.value = "option 2";

else event.value = "TEST";

 

Thank you

This topic has been closed for replies.
Correct answer bebarth

Hi,

 

I have sent you a message with the serial number breaks and the torque settings

 

Thank you


Hi,

In accordance with your table you sent me, here is a custom keystroke script which allows to only type a serial number at a correct format and display the corresponding torque:

function displaying(theValue) {
	if (theValue.length!="ABCD12345AHB15000".length) this.getField("torque").value="";
	else {
		if (/HB15\d{3}/.test(theValue)) {
			if (/[A-J]HB15\d{3}/.test(theValue) || /KHB1500[0-3]/.test(theValue)) this.getField("torque").value="1230Nm";
			else this.getField("torque").value="1570Nm";
		} else if (/HB1(1|2|3|4|7)\d{3}/.test(theValue)) {
			if (/[A-J]HB1(1|2|3|4|7)/.test(theValue) || /KHB1(1|2|3|4|7)00[0-3]/.test(theValue)) this.getField("torque").value="1355Nm";
			else if (/HB1(1|2)/.test(theValue)) this.getField("torque").value="1355Nm";
			else if (/HB1(3|4)/.test(theValue)) this.getField("torque").value="1670Nm";
			else this.getField("torque").value="1050Nm";
		} else this.getField("torque").value="Please check service manual";
	}
}
if (!event.willCommit) {
	event.change=event.change.toUpperCase();
	if (event.value.length=="ABCD12345".length && event.change!="") event.change+="HB";
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
	if (testeChaine.length<="ABCD12345".length) var RegExSerialNumber=/^([A-Z]{1,4}\d{0,5})?$/;
	else if (testeChaine.length<="ABCD12345AHB15".length) var RegExSerialNumber=/^([A-Z]{1,4}\d{0,5}([A-Z](H(B(((0(1|2|3|4|5|7)?)|(1(1|2|3|4|5|7)?)))?)?)?)?)?$/;
	else var RegExSerialNumber=/^([A-Z]{1,4}\d{0,5}([A-Z](H(B(((0(1|2|3|4|5|7)?)|(1(1|2|3|4|5|7)?))\d{0,3})?)?)?)?)?$/;
	if (RegExSerialNumber.test(testeChaine)) displaying(testeChaine);
	else event.rc=false;
} else {
	var RegExSerialNumber=/^[A-Z]{4}\d{5}[A-Z]HB((0(1|2|3|4|5|7))|(1(1|2|3|4|5|7)))\d{3}$/;
	if (event.value=="" || RegExSerialNumber.test(event.value)) displaying(event.value);
	else event.rc=false;
}

Attached is the example file.

@+

2 replies

Legend
April 6, 2023

You say Java, but this is JavaScript. It is very important to get this right, because if you try to get help, and say you use "Java" you will get wrong, confusing, or damaging advice!

 

Ok, you give us a code fragment. But you don't tell us what you want to happen ("expected result") and what actually happens ("actual result") for different values of Serial No. Also did you check the console for errors? Also, where is this script fragment located (including field and event type).

John CorfAuthor
Known Participant
April 6, 2023

Hi,

 

Apologies for the Java/ Java script error!

hopefully my last comment will make things slightly clearer! so currently unless you have for example AHB15 in the code the textbox will have "Test" in the textbox. the script is located in "custom calculation script" of the textbox

 

Thank you

bebarth
Community Expert
Community Expert
April 6, 2023

Hi,

Could you try to explain a bit better because at the moment I don't understand anything at all...

@+

John CorfAuthor
Known Participant
April 6, 2023

Hi,

 

Sorry it is a bit convolutated!

 

So I have a form for checking machinary. This form is used by engineers. Each time the engineer fills out the form they must put the serial number in a text box at the top of the page ("Serial No"). there are several textboxes for the enginner to coment in on the page. One of these (the text box in question) tells the enginner a tourqe rating required for a bolt.

depending on the machine/ serial number the tourque rating can change. A complete serial number looks something like this: ABCD12344AHB15000. this can be broken down to look like this though:

 

ABCD    Manufacturer

12344     Machine Model

A            Model year

HB15      Also machine model

000         Machine number off the production line

 

so the issue I am having is the tourqe rating for a bolt is 1200 Nm form AHB15000 to KHB15103 and 1600 Nm form KHB15104 onwards.

 

I can get the text box in question to search the "Serial No" textbox for items such as AHB15XXX AND CHB15XXX etc. fine but other than writing the following from 000 to 103 then 104 to 999 (code shown below) I dont know of a simpler way to do it. Im not sure its very efficient for the code to have 1000 lines for differnet serial numbers!

 

else if (/KHB15000/.test(s)) event.value = "1200Nm";
else if (/KHB15001/.test(s)) event.value = "1200Nm";
else if (/KHB15002/.test(s)) event.value = "1200Nm";

etc.

else if (/KHB15104/.test(s)) event.value = "1600Nm";

else if (/KHB15105/.test(s)) event.value = "1600Nm";

else if (/KHB15106/.test(s)) event.value = "1600Nm";

etc

 

Hopefully this make more sense! if not let me know please!

 

Thank you

bebarth
Community Expert
Community Expert
April 6, 2023

View the unit, I guess you want to say "torque" instead of "tourque"... 😉

If I understand well, you should only write:

 

 

if (/[A-J]HB15000/.test(s) || /KHB1500[0-3]/.test(s)) event.value = "1200Nm";
else  event.value = "1600Nm";

 

 

@+