2

我正在使用 ServiceNow 平台。我正在编写目录客户端脚本来验证目录项记录生成器上的表单字段。

如果验证未通过检查,我将使用 return false 停止提交表单。

我已经通过输入无效数据(带有特殊字符的组名或已经存在的组名)对此进行了测试,它发现了问题并显示了错误消息。我可以输入无效数据并多次提交,它可以工作。

但是,问题:

在我第一次输入无效数据并提交后,脚本似乎“停止”运行,然后我更正数据再次按下提交按钮。它只是坐在那里,什么也不做。我必须再次重新加载表格,这是不可取的。

控制流发生了什么?如果数据无效,如何彻底停止表单,然后允许用户更正错误并再次按下提交按钮继续?

我可以说脚本不会再次运行,因为每次脚本运行时都会弹出一个警告框,上面写着“脚本运行”。它只是在先提交无效数据然后输入一些有效数据并按提交后停止运行。

function onSubmit() {
    g_form.hideAllFieldMsgs('error');
    alert("script run");

    //Group Name contain letters numbers and dashes only
    var group_name = g_form.getValue('u_group_name');
    // Group name regular expression
    var regGrpName = /^([A-Za-z0-9\-]+)$/;
    // Check name against regular expression
    if (regGrpName.test(group_name) == false) {
        g_form.showFieldMsg('u_group_name', "Group Name must contain only letters, numbers or dashes. ", 'error');
        //Do not submit
        //g_form.submitted = false;
        return false;
    }

    //Check if google group already exists
    var rec = new GlideRecord('u_google_user_accounts');
    rec.addQuery('u_account_email', new_group_email);
    rec.query();

    while (rec.next()) {
        g_form.showFieldMsg('u_group_name',rec.u_account_email + " already exists as an account.",'error');
        return false;
    }

    //Group Members Email List separated by commas
    // Hide error message
    //g_form.hideErrorBox('u_group_members');
    var group_members = g_form.getValue('u_group_members');
    // Comma separate list
    var member_split = group_members.split(',');
    // Loop over list of email addresses
    for (var n = 0; n < member_split.length; n++) {
        // Trim whitespace
        var member_info = trim ? member_split[n].trim() : member_split[n];
        // Email validation regular expression
        var regEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
        // Check each item against regular expression
        if (member_info.search(regEmail) == false) {
            g_form.showFieldMsg('u_group_members', "Group Members contains an invalid email address. " + member_info, 'error');
            //Do not submit
            //g_form.submitted = false;
            return false; 
        } else if (member_info.search(validRegExp) == true) {
            g_form.setValue('u_group_members', group_members);  
        }
    }
    return true;
}
4

2 回答 2

1

很高兴您在上面找到了解决方案,但我也想发表评论,询问您是否尝试过 try{} catch{} 块来处理无效数据?

于 2015-05-31T21:56:52.373 回答
0

我想我已经解决了这个问题。我制作了一个完全独立的函数来检查验证。onSubmit 调用验证函数并检查返回值。如果返回值为 false,则停止表单。否则,即使在多次尝试使用无效数据后也会提交。我认为这会成功。让我知道是否有人可以看到任何问题。谢谢您的帮助。

function onSubmit() {
    var isValid = checkGoogleGroup();
    if (isValid == false) {
        g_form.submitted = false;
        return false;
    }
}
function checkGoogleGroup() {
    g_form.hideAllFieldMsgs('error');
    //Group Name contain letters numbers and dashes only
    var group_name = g_form.getValue('u_group_name');
    // Group name regular expression
    var regGrpName = /^([A-Za-z0-9\-]+)$/;
    // Check name against regular expression
    validGroupName = regGrpName.test(group_name);
    if (validGroupName == false) {
        g_form.showFieldMsg('u_group_name', "Group Name must contain only letters, numbers or dashes. ", 'error');
        //Do not submit
        return false;
        }
//Check if google group already exists
    var rec = new GlideRecord('u_broad_user_accounts');
    rec.addQuery('u_account_email', new_group_email);
    rec.query();
    while (rec.next()) {
        g_form.showFieldMsg('u_group_name',rec.u_account_email + " already exists as an account.",'error');
        return false;
    }
//Group Members Email List separated by commas
    var group_members = g_form.getValue('u_group_members');
    // comma separate list
    var member_split = group_members.split(',');
    // loop over list of email addresses
    for (var n = 0; n < member_split.length; n++) {
        // trim whitespace
        var member_info = trim ? member_split[n].trim() : member_split[n];
        // validation regular expression
        var validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
        // check each item against regular expression
        if (member_info.search(validRegExp) == -1) {
            g_form.showFieldMsg('u_group_members', "Group Members contains an invalid email address. " + member_info, 'error');
            return false;
        }
    }
}
于 2015-05-15T20:35:16.467 回答