简体   繁体   English

我们可以使用一个Java脚本函数对多个输入进行验证吗?

[英]Can we use one java script function to multiple input validation?

We created a JavaScript function to validate user name , it is working fine, but if i try to use this function to another input validation at the same jsp page, it is showing only text, i included text color in same function but it is not working in second input validation. 我们创建了一个JavaScript函数来验证用户名 ,它工作正常,但是如果我尝试在同一jsp页面上将此函数用于另一个输入验证,则它仅显示文本,我在同一函数中包括了文本颜色,但没有在第二次输入验证中工作。 is it possible to get?, if yes, how can i get?, i am starting level in java script, pls give your answer if anything, thank you. 是否有可能获得?,如果可以,我如何获得?,我正在用Java脚本入门,请给我答案(如果有的话),谢谢。

for example , this is my js function: 例如,这是我的js函数:

function name_validation()
      {
         if(true){
          var unam=document.getElementById("v2").innerHTML="valid";
          v2.style.color="green"; //this line is not exist in second input validation
                 }
         else{
            var unam=document.getElementById("v2").innerHTML="invalid";
            v2.style.color="red"; //this line is not exist in second input validation
              }
      }

this is my jsp code: 这是我的jsp代码:

name:<input type="text" id="name" onchange="name_validation();"><a id="v2"></a>
Father name:<input type="text" id="name" onchange="name_validation();"><a id="v2"></a>

You can pass current object like this 您可以像这样传递当前对象

name:<input type="text" id="name" onchange="name_validation(this);">
Father name:<input type="text" id="father_name" onchange="name_validation(this);">

then to do (JQuery): 然后做(JQuery):

function name_validation(obj) {
var value = $(obj).val();
...
}

for javascript 对于javascript

function name_validation(obj) {
var value = obj.value;
...
}

Try this way hope it helps : 尝试这种方式希望对您有所帮助:

Name:<input type="text" id="name" onchange="name_validation(this.id);">
Father name:<input type="text" id="father_name" onchange="name_validation(this.id);">

function name_validation(id){ 
  if(document.getElementById(id).value == ''){
    alert(id + 'is null');
  }

}

Try following. 请尝试以下。

Name:<input type="text" id="name" class="validate">
Father name:<input type="text" id="father_name" class="validate">
$(function(){
   $('.validate').on('blur',function(){
      if($(this).val() == '')
      {
         $(this).css({'border-color':'red','background':'somecolor '});
      }
      else{
         $(this).css({'background':'somecolor'});
      }   

   });

});

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

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