excel中满足条件的求和用什么函数 满足条件填写对应数据
本文旨在解决 .NET Core Razor Pages 中提交表单的控制问题,核心在于如何在客户端通过 JavaScript 验证表单数据解决,并仅在所有验证后才触发表单提交。我们将详细介绍修改现有的代码,利用 jQuery 的 Submit() 方法来实现这一目标,从而提高用户体验数据和质量。方案
问题的核心,现有的 JavaScript 验证函数validate() 无法阻止表单的提交,即使某些验证失败。要解决这个问题,我们需要修改 validate() 函数,通过显式地触发表单提交在所有验证中设置。
步骤 1:为表单添加 ID
首先,我们需要为 .cshtml 文件中的 lt;formgt;元素添加一个唯一的 ID,以便我们可以使用 JavaScript来它选择。
立即学习“前端免费学习笔记(研究)”;lt;form id=quot;myFormquot;class=quot;mb-3quot;method=quot;postquot;gt; ...lt;/formgt;登录后复制
步骤2:修改validate()函数
接下来,我们需要validate()函数,生成在所有验证函数都返回true时,使用jQuery的submit()方法来提交表单。由于原代码中的验证函数返回true/false的含义和正常理解的含义是相反的,这里统一修改为验证通过返回true,验证失败返回false。同时,验证函数也需要返回一个bool值,表示是否验证通过。
Munch
AI 营销分析工具,长视频中提取出最具吸引力的短片 85 查看详情 const fname=()=gt;{ const a=document.getElementById(quot;signUpFirstNamequot;).value; const new1= document.getElementById(quot;l1quot;); const new2=new1.getElementsByTagName(quot;spanquot;); const b=document.getElementById(quot;signUpFirstNamequot;).name; console.log(b); if(a.length===0) { new2[0].innerHTML=quot;请输入你的名字quot;; new2[0].style.color=quot;redquot;; return false; // 修改为验证失败返回 false } else { new2[0].innerHTML=quot;quot;; return true; // 修改为验证成功返回 true }}const lname=()=gt;{ const a=document.getElementById(quot;signUpLastNamequot;).value; const new1= document.getElementById(quot;l2quot;); const new2=new1.getElementsByTagName(quot;spanquot;); if(a.length==0) { new2[0].innerHTML=quot;请输入您的姓氏quot;; new2[0].style.color=quot;redquot;; return false; //修改为验证失败返回 false } else { new2[0].innerHTML=quot;quot;; return true; // 修改为验证成功返回 true }}const checkcountry
=()=gt;{ const a=document.getElementById(quot;signUpCountryCodequot;).value; const new1= document.getElementById(quot;l3quot;); const new2=new1.getElementsByTagName(quot;spanquot;); if(a.length==0) { new2[0].innerHTML=quot;请输入您的国家quot;; new2[0].style.color=quot;redquot;; return false; // 修改为验证失败返回 false } else { new2[0].innerHTML=quot;quot;; return true; // 修改为验证成功返回 true }}const checkMobile=()=gt;{ const a=document.getElementById(quot;signUpMobileNumberquot;).value; const new1= document.getElementById(quot;i4quot;) const new2=new1.getElementsByTagName(“span”;); constphoneno = /^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; console.log(a.length); if(!(a.match(phoneno))) { new2[0].innerHTML=quot;请输入有效的手机号码quot;; new2[0].style.color=quot;redquot;; count=1; return false; // 修改为验证失败返回 false } else { new2[0].innerHTML=quot;quot;; count=0; return true; // 修改为验证成功返回 true }}const checkEmail=()=gt;{ const a=document.getElementById(quot;signUpEmailquot;).value; var mailformat = /^[a-zA-Z0-9.!#$amp;'* /=?^_`{|}~-] @@[a-zA-Z0-9-] (?:.[a-zA-Z0-9-] )*$/; const new1= document.getElementById(“;i5quot;) const new2=new1.getElementsByTagName(“;spanquot;); if(!a.match(mailformat)) {
console.log(quot;uiquot;) new2[0].innerHTML=quot;请输入有效的邮箱地址。quot;; new2[0].style.color=quot;redquot;; count=1; return false; // 修改为验证失败返回 false } else { new2[0].innerHTML=quot;quot;; count=0; return true; // 修改为验证成功返回 true }}const checkpass1=()=gt;{ const a=document.getElementById(quot;signUpPasswordquot;).value; const new1= document.getElementById(quot;i6quot;) const new2=new1.getElementsByTagName(quot;spanquot;); constspecialChars = /[`!@@#$^amp;*()_ -=[]{};':quot;\|,.lt;gt;/?~]/; if(a.lengthlt;8) { new2[0].innerHTML=quot;密码长度必须至少8 个字符quot;; new2[0].style.color=quot;redquot;; count=1; console.log(quot;修改countquot;,count); return false; // 为验证失败返回 false } else if(a.lengthgt;12) { new2[0].innerHTML=quot;密码长度不能超过12个字符quot;; new2[0].style.color=quot;redquot;; count=1; return false; //修改为验证失败返回 false } else if(!specialChars.test(a)) { new2[0].innerHTML=quot;密码必须包含至少1个特殊字符";; new2[0].style.color=quot;redquot;; count=1; return false; // 修改为验证失败返回 false } else{ new2[0].innerHTML=quot;quot;; count=0; console.log(quot;countquot;,count); ret
urn true; // 验证成功返回 true }}const checkpass2=()=gt;{ const a=document.getElementById(quot;signUpConfirmPasswordquot;).value; const b=document.getElementById(quot;signUpPasswordquot;).value; const new1= document.getElementById(quot;i7quot;) const new2=new1.getElementsByTagName(quot;spanquot;); if(a!=b) { new2[0].innerHTML=quot;密码不匹配quot;; new2[0].style.color=quot;redquot;; count=1; return false; // 修改为验证失败返回 false } else { new2[0].innerHTML=quot;quot;; count=0; return true; // 修改为验证成功返回 true }}const validate=()=gt;{ const isFirstNameValid = fname(); const isLastNameValid = lname(); const isMobileValid = checkMobile(); const isCountryValid = checkcountry(); const isEmailValid = checkEmail(); const isPassword1Valid = checkpass1(); const isPassword2Valid = checkpass2(); if(isFirstNameValid amp;amp; isLastNameValid amp;amp; isMobileValid amp;amp; isCountryValid amp;amp; isEmailValid amp;amp; isPassword1Valid amp;amp; isPassword2Valid) { console.log(quot;alight go ahead.quot;); $(quot;#myFormquot;).submit(); return true; //返回 true 表示验证通过 } else { console.log(quot;错误,请勿提交表单!!quot;); //document.getElementById(quot;submitButtonquot;).setAttribute('disabled','disabled'); return false; //返回 fals
e 表示验证失败 }}登录后复制
步骤3:修改按钮的onclick事件
由于我们现在需要在validate()函数中处理表单提交,所以我们需要修改按钮的onclick事件,生成在验证通过后才提交表单。同时,由于submit按钮默认是提交行为表单,所以不需要指定type="submit"。lt;button onclick=quot;return validate()quot;class=quot;btn btn-primary rounded-pillquot;id=quot;submitButtonquot;gt;Sign Uplt;/buttongt;登录后复制
注意事项确保您的项目中包含 jQuery在实际项目中,你可能需要更复杂的验证逻辑,例如异步验证或服务器端验证。为了更好的用户体验,你可以在验证失败时,显示更详细的错误信息。请注意代码中的注释,它们解释了每个步骤的目的和作用。
总结
通过以上步骤,我们成功修改了.cshtml 方法可以提高用户体验,减少服务器端的负载,并保证数据的完整性。记住,客户端只是安全措施的一部分,服务器端验证仍然是必需的。
以上就是确保所有条件满足时才提交 .cshtml 表单的详细信息,更多请关注乐哥意见网其他相关内容文章! 相关标签: javascript word java jquery html go ai 提交 red JavaScript jquery bool 事件异步