Javascript sapui5中必需的字段验证器
我有四个文本字段,标签有姓名、年龄、城市和电话号码。如果它为空,我必须验证它。它应该提醒我去填。如何在sapui5中使用必需的字段验证器验证文本字段 您只需编写一个函数来获取文本字段并检查其值。Javascript sapui5中必需的字段验证器,javascript,jquery,sapui5,Javascript,Jquery,Sapui5,我有四个文本字段,标签有姓名、年龄、城市和电话号码。如果它为空,我必须验证它。它应该提醒我去填。如何在sapui5中使用必需的字段验证器验证文本字段 您只需编写一个函数来获取文本字段并检查其值。 这可能是这样的: function validateTextFieldValues() { // this function gets the first textfield var nameTextField = sap.ui.getCore().byId("idOfTheTextF
这可能是这样的:
function validateTextFieldValues() {
// this function gets the first textfield
var nameTextField = sap.ui.getCore().byId("idOfTheTextFieldContaingTheName");
//this function checks its value, you can insert more checks on the value
if(nameTextField.getValue() === "") {
alert("Please enter a name.");
}
// ...
// the same for the other fields
}
然后,您可以在单击按钮时绑定函数,例如,在创建按钮时:
new sap.ui.commons.Button({
// your buttonconfiguration
click: function(){
validateTextFieldValues();
}
});
此外,
TextField
还有一个名为valueState
的属性关于其事件
liveChange
,有机会在键入时验证输入:
new sap.ui.commons.TextField({
// your configuration
liveChange: function(){
if(this.getValue() === "")
this.setValueState(sap.ui.core.ValueState.Error);
else
this.setValueState(sap.ui.core.ValueState.Success);
}
});
()您可以在更改字段值本身时对其进行验证,如下所示
var oname = new sap.ui.commons.TextField({
id: 'input2',
change : function(){
if(this.getValue()=='')
alert("enter some value");
}
});
或者您可以在单击某个按钮时编写验证函数。即使使用必需的属性也没有帮助,因为UI5不会将任何控件放入表单标记中。必需的属性设置一个属性 aria required=true 您可以使用jQuery选择所有aria必需的元素,并在任何其他控件事件(如按下按钮)上处理它们 下面是相同的示例代码
var oBtn = new sap.ui.commons.Button();
oBtn.attachPress(function(){
var error;
jQuery('input[aria-required=true]').each(function(){
var oInput = sap.ui.getCore().byId(this.id);
var val = oInput.getValue();
if(!val){
var sHtml = '<ul><li><strong> value is empty</li></ul>';
var sValueState = '<strong>Error!</strong>';
// Combine Input value, Error title and message into a Rich Tooltip
var oTooltip = new sap.ui.commons.RichTooltip({ text: sHtml, valueStateText: sValueState});
oInput.setTooltip(oTooltip);
oInput.setValueState(sap.ui.core.ValueState.Error);
error = false;
}else{
oInput.setValueState(sap.ui.core.ValueState.None);
oInput.setTooltip(' ');
}
});
if(error){
return error;
}
});
var oBtn=new sap.ui.commons.Button();
获取附件(函数(){
var误差;
jQuery('input[aria required=true]')。每个(函数(){
var oInput=sap.ui.getCore().byId(this.id);
var val=oInput.getValue();
如果(!val){
var sHtml='- 值为空;
var sValueState='错误!';
//将输入值、错误标题和消息组合到丰富的工具提示中
var otoltip=new sap.ui.commons.RichTooltip({text:sHtml,valueStateText:sValueState});
oInput.setTooltip(oTooltip);
oInput.setValueState(sap.ui.core.ValueState.Error);
错误=错误;
}否则{
oInput.setValueState(sap.ui.core.ValueState.None);
oInput.setTooltip(“”);
}
});
如果(错误){
返回误差;
}
});
sapui5中是否有任何用于验证的API引用?如果(this.getValue().trim().length==0){}1。一些UI5控件也有2个。UsePlus似乎是输入处理的内置方式,但我看不到比自定义错误处理功能更大的好处。