如果条件逻辑不工作?
If condition logic Not working?
if (!this.state.username.match(/^[a-zA-Z0-9._-]+@(?!gmail.com)(?!yahoo.com)(?!outlook.com)(?!aol.com)(?!live.com)(?!hotmail.com)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
alert("Only Business Emails Are Allowed");
}
if (!this.state.phonenumber.match(/^[0-9]{10}$/)) {
alert("10 digit mobile number or 10 digit Area code followed by landline number only allowed");
} else {
let data = Object.assign({
profilePhoto: (this.state.files && this.state.files.length == "0") ? "" : this.state.files[0].base64,
}, this.state);
UserAction._createUser(data, (data) => {
console.log(data);
(data.response == "SUCCESS") ? this.setState({ gotSuccess:true, openrejectDialog: true}) : alert(data.message)
});
}
我正在根据用户名和电话号码的匹配情况检查条件。
如果用户名或电话号码不匹配,我会收到警报。然后在警报中单击 "ok" 它将转到其他
我希望在 "if" 条件
成功完成后调用 else 语句
基本上你做的不对,你应该在每次验证时使用 if/else,最后一个案例就是你的成功调用。
根据您的代码,如果您的用户名错误而号码正确,您将调用 createUser
功能,这不是所需的流程。
下面你有一个工作代码:
如果用户名错误,显示错误。
如果用户名正确,请检查号码是否正确,如果不正确,则显示错误。
默认情况下如果所有检查都通过。
if (!this.state.username.match(/^[a-zA-Z0-9._-]+@(?!gmail.com)(?!yahoo.com)(?!outlook.com)(?!aol.com)(?!live.com)(?!hotmail.com)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
alert("Only Business Emails Are Allowed");
} else if (!this.state.phonenumber.match(/^[0-9]{10}$/)) {
alert("10 digit mobile number or 10 digit Area code followed by landline number only allowed");
} else {
let data = Object.assign({
profilePhoto: this.state.files && this.state.files.length == "0" ? "" : this.state.files[0].base64,
}, this.state);
UserAction._createUser(data, (data) => {
console.log(data);
data.response == "SUCCESS" ? this.setState({
gotSuccess: true,
openrejectDialog: true
}) : alert(data.message)
});
}
您正在使 else
代码仅以第二个 if
语句为条件。
很快,else
代码不依赖于第一个 if
。
你可以试试这个,只有当两个 if
都是 false 时才能 运行 else
:
if( <checks if the email is valid>){
alert("Only Business Emails Are Allowed");
} else if ( <checks the phone number>){
alert("10 digit mobile number or 10 digit Area code followed by landline number only allowed");
} else {
/* the else code here */
}
if (!this.state.username.match(/^[a-zA-Z0-9._-]+@(?!gmail.com)(?!yahoo.com)(?!outlook.com)(?!aol.com)(?!live.com)(?!hotmail.com)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
alert("Only Business Emails Are Allowed");
}
if (!this.state.phonenumber.match(/^[0-9]{10}$/)) {
alert("10 digit mobile number or 10 digit Area code followed by landline number only allowed");
} else {
let data = Object.assign({
profilePhoto: (this.state.files && this.state.files.length == "0") ? "" : this.state.files[0].base64,
}, this.state);
UserAction._createUser(data, (data) => {
console.log(data);
(data.response == "SUCCESS") ? this.setState({ gotSuccess:true, openrejectDialog: true}) : alert(data.message)
});
}
我正在根据用户名和电话号码的匹配情况检查条件。 如果用户名或电话号码不匹配,我会收到警报。然后在警报中单击 "ok" 它将转到其他 我希望在 "if" 条件
成功完成后调用 else 语句基本上你做的不对,你应该在每次验证时使用 if/else,最后一个案例就是你的成功调用。
根据您的代码,如果您的用户名错误而号码正确,您将调用 createUser
功能,这不是所需的流程。
下面你有一个工作代码:
如果用户名错误,显示错误。
如果用户名正确,请检查号码是否正确,如果不正确,则显示错误。
默认情况下如果所有检查都通过。
if (!this.state.username.match(/^[a-zA-Z0-9._-]+@(?!gmail.com)(?!yahoo.com)(?!outlook.com)(?!aol.com)(?!live.com)(?!hotmail.com)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
alert("Only Business Emails Are Allowed");
} else if (!this.state.phonenumber.match(/^[0-9]{10}$/)) {
alert("10 digit mobile number or 10 digit Area code followed by landline number only allowed");
} else {
let data = Object.assign({
profilePhoto: this.state.files && this.state.files.length == "0" ? "" : this.state.files[0].base64,
}, this.state);
UserAction._createUser(data, (data) => {
console.log(data);
data.response == "SUCCESS" ? this.setState({
gotSuccess: true,
openrejectDialog: true
}) : alert(data.message)
});
}
您正在使 else
代码仅以第二个 if
语句为条件。
很快,else
代码不依赖于第一个 if
。
你可以试试这个,只有当两个 if
都是 false 时才能 运行 else
:
if( <checks if the email is valid>){
alert("Only Business Emails Are Allowed");
} else if ( <checks the phone number>){
alert("10 digit mobile number or 10 digit Area code followed by landline number only allowed");
} else {
/* the else code here */
}