简体   繁体   English

验证两个输入之间的一个输入

[英]Validate one input between two inputs

I have javascript validation code. 我有JavaScript验证码。 I want to validate one input between two inputs. 我想验证两个输入之间的一个输入。 More clearly, 更清楚,

if a text input entered and dropdown input remains unselected, the js should validate the input and form should be submitted and if dropdown selected and text input not entered than dropdown should be validate but not text input and form should be submitted. 如果输入的文本输入和下拉输入保持未选中状态,则js应该验证输入并提交表单,并且如果选择了下拉列表并且未输入文本输入而不是下拉列表则应该得到验证,但文本输入和表单不应该提交。 I have simple validation and new to this problem. 我有简单的验证和新的问题。

if(document.form.inputname.value==""){
alert("Please Enter Your Name");
return false;}

if (form.inputname2.selectedIndex==0){
alert("Please select Choice !");
return(false);}

Update : In simple words I want to validate one input at one time (note: second input is hide through css and show when user click on a link) Now suppose user click link then first input will be hide and second show, but with my above code alert in action for both input. 更新:简单地说,我想一次验证一个输入(注意:第二个输入是通过css隐藏并在用户单击链接时显示)现在假设用户单击链接,则第一个输入将是hide和第二个显示,但是使用我的以上代码对两个输入均起作用。 So how I can validate either first input or second input. 因此,我如何验证第一输入或第二输入。 Is this possible with javascript or jquery ? 这可能与javascript或jquery吗?

Try this: 尝试这个:

   $("#Submit").on("click", function () {

     var dVal= $('#dropdownId').find('option:selected').val();
     var txt= $('#textbox').val();

     if(dVal!=''&& txt==''){
       alert("Please Enter Your Name");
       retrun false;
     }
     else if(dVal==''){
        alert("Please select Choice !");
        return(false);
     }
      else{
           //put your form submit code here
       }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM