如何在文本字段上验证以下条件,并使用 JavaScript 验证显示满足所有条件的弹出页面

how to validate the below criteria on the text fields and display the pop up page on satisfying all the condition using javascript validation

本文关键字:验证 有条件 显示 JavaScript 满足 字段 文本 条件      更新时间:2023-09-26
   **Below is the input text fileds**
<form:form commandName="DRCNdetails"  id="frm1" method="POST" action="addNewDelayReason.do" >
       <form:input id="text1" path="delayCategory" cssClass="padR10 boxSizing"   maxlength="75"></form:input>
      <form:input path="preFix"  id="text2" cssClass="padR10 boxSizing" maxlength="6">            </form:input>
         <form:input path="reasonValue" maxlength="150" id="reasonValue"    cssClass="textbox width100" cssStyle="visibility:hidden"></form:input>
                  </form:form> <button class="btnStyle blueBtn"                         onclick="formvalidation(),validateSpecialCharacters()"> <span class="left"> 

提交

提交时需要提供ID的"text1","text2"文件,验证成功后应显示以下弹出屏幕,即showLtBox('mask', 'addMisdReasonCode1'),条件如下:

             should not allow the null values for both the fileds, and special characters,and integers  and alert me accordingly , on satisying this criteria only it should display the pop up screen showLtBox('mask', 'addMisdReasonCode1')

据我了解这个问题,您需要创建一个在按下提交按钮时触发的验证函数。

我已经看到您已经创建了将在"OnClick"操作时触发的功能,但是语法不正确。

  1. 您需要使用 ";" 而不是 "," 来分隔这两个函数
  2. 您只需要使用一个函数进行验证 - 这将比您编写的代码更具可读性。
  3. 结果应返回到 onclick 命令。

例:按如下所示更改属性 onclick:

onclick="return FullValidation();"

创建新的 JavaScript 函数,其中包含两个验证并返回验证结果;

function FullValidation() {
 var validationResultOK = formvalidation();
 if (validationResultOK) {
    validationResultOK  = validateSpecialCharacters();
 }
 if (validationResultOK) {
  // Popup alert should be here, I put alert as example.
  alert("Out your message here");
 }
 return validationResultOK;
}

使用 Javascript 正则表达式。

var pattern = new RegExp(/^[a-zA-Z]{n}$/);

其中n是您要接受的追逐者的数量。

上面的正则表达式仅匹配 charecters(两种情况)。

现在从text fields中检索值

var text1 = document.getElementById("#your_id").value;
var text2 = document.getElementById("#your_id").value;
if(pattern.test(text1) && pattern.test(text2))
{
// your popup code
}