我有一個驗證表單輸入的函數,我需要返回驗證的值。現在,我使用if else
子句調用個別驗證器,所以我的函數僅返回第一個驗證字段(age
),但我想將age
,lastName
和firstName
返回給調用函數。如何從JavaScript函數返回多個值
如何獲取多個要返回的值?
validationForm(numberOfPassengers){
for (let i=0; i < numberOfPassengers; i++) {
let firstName = ReactDOM.findDOMNode(this.refs['firstName'+typeOfPassenger+i]).value;
let lastName = ReactDOM.findDOMNode(this.refs["lastName"+typeOfPassenger+i]).value
let age = ReactDOM.findDOMNode(this.refs['age'+typeOfPassenger+i]).value;
let regex = /^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/;
let resultFirstName = firstName.match(regex);
let resultLastName = firstName.match(regex);
if(age <= 0){
('.age-alignment').addClass('error-border');
}else{
return age;
};
if(resultFirstName){
return firstName;
}else{
('.first-name').addClass('error-border');
};
if(resultLastName){
return lastName;
}else{
('.last-name').addClass('error-border');
};
}
}
如果您使用React,則不要手動操作DOM。 – dandavis
可以請你詳細說明 –