【问题标题】:salesforce apex - string must be 6 digits and numeric onlysalesforce apex - 字符串必须是 6 位数字,并且只能是数字
【发布时间】:2021-11-25 10:16:28
【问题描述】:
在这个顶点代码中,预期的输出没有得到问题是
取一个字符串类型的变量
string pincode = '500038';
- Pincode 必须是 6 位数字,如果位数不等于 6 则打印 "Pincode must be 6 digits"
- Pincode 只能是数字,如果用户输入字母,则会显示错误消息“Pincode 只能是数字”
我试过这段代码
string pincode = '500038';
if (pincode.isNumeric()) {
system.debug('Pincode must be numeric only');
if (pincode.len == 6) {
system.debug('Pincode must be 6 digits');
}
}
【问题讨论】:
标签:
if-statement
apex
apex-code
【解决方案1】:
我希望这能解决您的问题,在第一个条件下,我们正在检查数字密码或不检查密码,在子条件下,我们检查密码的长度是否为 6,最后在其他情况下,如果密码密码,我们将打印 msg不是数字。
.
string pincode = '500038';
if (pincode.isNumeric()) {
system.debug('Pincode is be numeric only');
if(pincode.length() != 6) {
system.debug('Pincode must be 6 digits');
}
if(pincode.length() == 6){
system.debug('Pincode is numeric and has 6 digits::'+pincode);
}
}else{
system.debug('Pincode contains other characters then number::'+pincode);
}