代码之家  ›  专栏  ›  技术社区  ›  Aditya

如何根据javascript中的输入值调用函数?

  •  0
  • Aditya  · 技术社区  · 8 年前

    下面是我的HTML示例代码,其中有一个接受数字和字母的输入字段。

    我需要根据用户输入的值调用函数,即如果用户输入纯10 dgits电话号码,则调用函数number(),如果用户输入电子邮件调用函数email(),则调用函数number(),否则混合,则出错。

    我只需要一个输入元素来输入电话号码和电子邮件。

    HTML

    <div>
    <input type="text" id="contact"/>
    <button onClick()="???">Submit</button>
    </div>
    
    <script>
    function number(){
      // call when input value is a number;
    }
    
    function email() {
     // call this when input value is an email;
    }
    </script>
    
    4 回复  |  直到 8 年前
        1
  •  1
  •   Ankit Agarwal    8 年前

    你可以用 regex 用于检查电子邮件值并使用 parseInt() 通过长度验证检查电话号码值

    function checkInput(){
      var contact = document.getElementById('contact').value;
      if(contact.length === 10 && parseInt(contact)){
        number();
      } else if(!parseInt(contact)){
        var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if(reg.test(contact)){
           email();
        } else {
           mix();
        }
      } else {
         mix();
      }
      
    }
    function number(){
      console.log("number");
    }
    
    function email() {
     console.log("email");
    }
    
    function mix() {
      console.log("Error");
    }
    <div>
    <input type="text" id="contact"/>
    <button onClick="checkInput();">Submit</button>
    </div>
        2
  •  2
  •   A. Llorente    8 年前
    <button onclick="number()">Submit</button>
    

    如果您不知道要调用哪个函数,则需要确定:

    HTML:

    <button onclick="decideFunction()">Submit</button>
    

    JS:

    function decideFunction() {
        const value = document.getElementById('contact').value;
        if (!Number.isNaN(value)) {
            number();
        } else if (isEmail(value)) { // check with regexp
            email(); //etc..
        }
    }
    
        3
  •  1
  •   Sebastian Speitel    8 年前

    只需调用另一个函数,检查该值是否为数字,并相应地执行以下操作:

    function func() {
      let val = document.getElementById("contact").value;
      if (!isNaN(parseInt(val))) number();
      else email();
    }
    
    function number() {
      // call when input value is a number;
      console.log("number() called");
    }
    
    function email() {
      // call this when input value is an email;
      console.log("email() called");
    }
    <input type="text" id="contact" />
    <button onclick="func()">Submit</button>
        4
  •  0
  •   Sanju Kaniyamattam    8 年前
    Try this....
    
     <div>
        <input type="text" id="contact"/>
        <button onClick()="decideFun();">Submit</button>
        </div>
    
        <script>
        function decideFun(){
          var inputVal = $("#contact").val();
    
          if($.isNumeric(inputVal)==true) number();
          else email();
        }
        function number(){
          // call when input value is a number;
        }
    
        function email() {
         // call this when input value is an email;
        }
        </script>