I have a form, there is a field called uppercase_value. In that field, I put @UpperCase(uppercase_value) in Input Translation because I need the value in that field is uppercase only.
In the xpage, I have a edit box and a button. The edit box uses simple data binding (bind to uppercase_value) and the button is for save the value.
Now I have to prevent the button saves duplicate value or empty value, I put a computed field near the edit box and put the following code in the button.
//get the value from the edit box
var newvalue = getComponent("inputText1").getValue();
//if edit box is empty, show message in the computed field
if(newvalue =="" || newvalue ==null)
{
getComponent("computedField1").setValue("The field is empty");
}
else
{
//check the value whether it is already existed or not
//use @DbLookup to find, newvalue is the keyword
var existedvalue = @DbLookup(@DbName(),"myview", newvalue,3 );
//if the newvalue is same as the existedvalue, show message in the computed field
if(newvalue == existedvalue)
{
getComponent("computedField1").setValue("Duplicate value");
}
else
{
document1.save();
}
}
I run the code, I type the value in lowercase, it saves it in uppercase as expected (for example, I type abc in edit box, it returns the value is ABC). I refresh the xpage and type the same value again, it can save the value and I can find there are two same value in the view.
I double check the view in @DbLookup, the first column is sorted so I am pretty sure the @DbLookup works fine.
I think the reason it can save duplicate value is in run form validation, I choose on document save. Therefore everytime I click the save button after type some values in the edit box, it changes the value to uppcase, then saves it. So if I refresh the page and type some value again, since I type in lowercase, the @DbLookup "thinks" it is a new value so it saves the value and thus there are duplicate values in the view.
But if I change on document save to on document load in run form validation. It does not work. I mean it does not change the value to uppercase and saves the lowercase value.
Actually I have another idea about change to uppercase. I intend to use onkeypress event in the edit box. I try to force the user to turn on the caps locks button when input value in the edit box
I follow this table and put the following code in the onkeypress event.
if (thisEvent.keyCode == 20 )
{
//alert("caps lock button pressed");
return true; // not work, still can type lowercase
// event.returnValue = true; // cannot type anything
}
else
{
return false;
// event.returnValue = false; // cannot type anything
}
However, it does not work properly, I can type lowercase in the edit box.
So how can I save the value in uppercase and prevent to save duplicate value?