使用JavaScript和jQuery验证具有多个字段的表单
我在SharePoint列表表单的使用JavaScript和jQuery验证具有多个字段的表单,javascript,jquery,sharepoint,Javascript,Jquery,Sharepoint,我在SharePoint列表表单的newform.aspx中有一个表单,包含40多个字段,在单击提交按钮时,我需要对这些字段执行验证。 我在SP Designer中编写如下内容: function PreSaveAction() { var fnamevalthrusputility = SPUtility.GetSPField('fisrt name').GetValue(); if(fnamevalthrusputility == n
newform.aspx
中有一个表单,包含40多个字段,在单击提交按钮时,我需要对这些字段执行验证。
我在SP Designer中编写如下内容:
function PreSaveAction()
{
var fnamevalthrusputility =
SPUtility.GetSPField('fisrt name').GetValue();
if(fnamevalthrusputility == null ||
fnamevalthrusputility.length === 0)
{
flagG =false;
//$("input[title='first name']
//").css('border-color','red');
alert('Enter first name.');
}
custid = SPUtility.GetSPField('Customer ID').GetValue();
if(custid == null || custid.length === 0)
{
flagG =false;
// document.contactinfo.Name.style.border="solid 1px red";
//jQuery("input[Title='Customer ID Required
field']").css("border","solid 1px red");
alert('Enter Customer ID.');
}
if(flagG == false)
{
alert('please fill mandatory fields');
return false;
}
else
{
// alert('Valdiations are done! Going to save the form //with a
// new id in the database');
return true;
}
但是当涉及多个字段时,我需要显示一条常见的错误消息,其中包含所有抛出错误的字段,即下拉字段中的未选择值为空等。
目前,我正在为每个已验证的字段显示不同的
alert
消息。我建议使用elementsTry上的required
和pattern
属性,以获取一个公共全局变量,在每个步骤(即更改其值)中填充所有这些错误,然后在整个过程结束后,将其放入单个警报功能中。你会得到准确的结果的。阿克沙亚,你能详细说明一下吗。比如var message=“”;如果(条件1){message+=“some error message”}
else如果(条件2){message+=“other type of error”}
那么最后您可以发出警报(message)
让我来处理这个问题并恢复。感谢您的时间和帮助。我建议使用elementsTry上的required
和pattern
属性,在每个步骤中使用一个通用全局变量来填充所有这些错误,即更改它的值,然后在整个过程结束后将其放入一个警报函数中。你会得到准确的结果的。阿克沙亚,你能详细说明一下吗。比如var message=“”;如果(条件1){message+=“some error message”}
else如果(条件2){message+=“other type of error”}
那么最后您可以发出警报(message)代码>让我来处理这个问题并回复。感谢您的时间和帮助。