如何验证共享点列表中的表单字段? [英] how do I validate form fields in sharepoint list?

查看:74
本文介绍了如何验证共享点列表中的表单字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在添加/编辑时验证列表项的字段并停止保存操作,并提供用户在该页面上所做的错误信息.

I want to validate the fields of the list item while adding/editing and stop the saving operation and provide the error information what the user made on that page itself.

例如,如果我想防止用户在保存之前根据其他字段的特定状态保留一些字段设置,则不能将该字段设为必填字段.

For ex, if I want to prevent user not to leave few fields set before saving based on particular status of another field, I cannot make the field as mandatory.

推荐答案

使用PreSaveAction.

Use PreSaveAction.

在页面上添加一个名为PreSaveAction的javascript函数(最好是创建自定义列表模板,并且可以修改将用作EditForm和NewForm的aspx页面,否则尝试使用Content Editor Web部件或通过修改Master页面),然后从那里进行所有自定义验证.

Add a javascript function named PreSaveAction to your page (it's best if you are creating a custom List Template and can modify the aspx page that will be used as EditForm and NewForm, otherwise try the Content Editor Web Part or by modifying the Master Page) and do all your custom validation from there.

例如,我只是在一个项目中使用了它,我们有3%的字段必须等于100%.我使用了以下JavaScript,效果很好:

For example, I just used it on a project where we had three percent fields that had to equal 100%. I used the following javascript and it worked great:

function getTagFromIdentifierAndTitle(tagName, identifier, title) {
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);
    for (var i = 0; i < tags.length; i++) {
        var tempString = tags[i].id;
        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
            return tags[i];
        }
    }
    return null;
}

function PreSaveAction() {
    var top = getTagFromIdentifierAndTitle("input", "TextField", "Top %");
    var middle = getTagFromIdentifierAndTitle("input", "TextField", "Middle %");
    var bottom = getTagFromIdentifierAndTitle("input", "TextField", "Bottom %");
    var valid = (100 == parseInt(top.value) + parseInt(middle.value) + parseInt(bottom.value));
    if (!valid) {
        alert("Top %, Middle %, and Bottom % must equal 100% when added.");
    }
    return valid;
}

这篇关于如何验证共享点列表中的表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆