简体   繁体   English

如何使用Java Script实时验证表单

[英]How to validate a form in real time using Java Script

http://jsfiddle.net/ZQQS9/2/ http://jsfiddle.net/ZQQS9/2/

I want to put green ticks next to correct inputs as users fill them up 当用户填写输入时,我想在正确的输入旁边放置绿色标记

here's how I have everything set up: 这是我如何设置所有内容:

<label>
<select class="allselect" onChange="check(this.name)" name="typeselect" id="typeselect">
<option value="off">Select...</option>
<option value="normalshoes">normalshoes</option>
<option value="smallshoes">smallshoes</option>
<option value="bigshoes">bigshoes</option>
<option value="coats">coats</option>
<option value="regularpants">regularpants</option>
<option value="bigpants">bigpants</option>
<option value="tshirt">tshirt</option>
<option value="long">long</option>
<option value="big">big</option>
<option value="dress">dress</option>
<option value="hats">hats</option>
<option value="bags">bags</option>
<option value="glasses">glasses</option>
<option value="other">other</option>    
</select> 
</label>
<div id="typeselectIMG" class="status"><img src="http://oi62.tinypic.com/2d0l214.jpg" alt="OK" class="IMGself"></div>
<div id="secondstep">
<input type="text" class="quantity" onKeyPress="check(this.name)"  name="quantity" id="quantity">    
<div id="quantityIMG" class="status"><img src="http://oi62.tinypic.com/2d0l214.jpg" alt="OK" class="IMGself"></div>
</div>

and JS: 和JS:

function check(id) {
        var idValue = document.getElementsByName(id)[0];
        var Valueit = idValue.value;
                if (id == 'typeselect') {
                    if (idValue != '' && Valueit != 'off') {
                    document.getElementById(id + 'IMG').style.visibility = 'visible';
                    document.getElementById('secondstep').style.visibility = 'visible';
                    }
                    if (Valueit == 'off') {
                    document.getElementById(id + 'IMG').style.visibility = 'hidden';
                    document.getElementById('quantityIMG').style.visibility = 'hidden';
                    document.getElementById('secondstep').style.visibility = 'hidden';

                    }
                }
                if (id == 'quantity') {
                    function testallthetime() {
                    if (Valueit != '') {
                    document.getElementById(id + 'IMG').style.visibility = 'visible';
                    document.getElementById('secondstep').style.visibility = 'visible';
                    }
                    if (Valueit == '') {
                    document.getElementById(id + 'IMG').style.visibility = 'hidden';
                    document.getElementById('secondstep').style.visibility = 'visible';
                    }
                    }
                    setInterval(testallthetime(),1);

                }
        }

Note: first part of JS regarding the select is working correctly as it should, I just can't get the empty text field to stop the green tick from showing. 注意:关于select的JS的第一部分正常工作,我只是不能得到空文本字段来阻止显示绿色勾号。

eg when you enter anything in the box, the green tick comes in, so far so good. 例如,当您在框中输入任何内容时,绿色勾号会出现,到目前为止一切都很好。 but if you delete everything you typed and you're left with nothing, the tick is still there, UNLESS you push backspace or del one more time. 但是如果你删除你输入的所有内容而你没有任何东西,那么勾号仍然存在,除非你再次推迟退格或del。 How can I make this in actual real time for it to detect it and when nothing is there not show the green tick? 我怎样才能在实时实时检测它,什么时候没有显示绿色勾? and vice versa. 反之亦然。 Any help is appreciated. 任何帮助表示赞赏。

http://jsfiddle.net/ZQQS9/2/ http://jsfiddle.net/ZQQS9/2/

jsFiddle Demo

There are a few things going on here. 这里有一些事情发生。 First, do not use a timer to check for input changes, it is really bad practice. 首先,不要使用计时器来检查输入变化,这是非常糟糕的做法。 It is harsh on the user's computer and too much of it will jar the user experience. 它对用户的计算机来说很苛刻,太多会影响用户体验。

Further, it is best practice to use unobtrusive javascript. 此外,最佳做法是使用不引人注目的javascript。 This means factoring out your event handlers into the code instead of on the elements. 这意味着将事件处理程序分解为代码而不是元素。 In order to do that, you are going to want to wait for the page to load (inside of a window.load event will work). 为了做到这一点,你将要等待页面加载(在window.load事件内部将工作)。

To recap, use oninput to check for changes, remove the timer, use unobtrusive javascript inside of an onload event, and you should be good. 回顾一下,使用oninput来检查更改,删除计时器,在onload事件中使用不显眼的javascript,你应该是好的。 Here is how it should look: 以下是它的外观:

//onload event
window.onload = function(){
  //get elements  
  var typeselect = document.getElementById("typeselect");
  var typeImg = document.getElementById('typeselectIMG');
  var secondstep = document.getElementById("secondstep"); 
  var quantInput = document.getElementById("quantity");
  var quantImg = document.getElementById("quantityIMG");
  //select event
  typeselect.onchange = function(){
   if( this.value != 'off' ){
    typeImg.style.visibility = 'visible';
    secondstep.style.visibility = 'visible';
   }else{
    typeImg.style.visibility = 'hidden';
    quantImg.style.visibility = 'hidden';
    secondstep.style.visibility = 'hidden';
   }
  };
  //input changed event
  quantInput.oninput = function(){
    if(this.value != ''){
     quantImg.style.visibility = 'visible';     
    }else{
     quantImg.style.visibility = 'hidden';
    }
  };
};

Wrap the body of check() in a call to setTimeout. 在调用setTimeout时包装check()的主体。 The event handler fires before the new value of the field is available, allowing you to prevent it from being applied. 事件处理程序在字段的新值可用之前触发,允许您阻止应用它。 Calling setTimeout schedules your code to execute a moment later, when the value is available. 调用setTimeout会调度您的代码,以便在值可用时执行。

There is no need for the setInterval later on, calling testallthetime. 稍后不需要setInterval,调用testallthetime。 You set Valueit in the outer scope and it's not going to change no matter how many times you check it. 您在外部范围内设置Valueit,无论您检查多少次,它都不会改变。

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

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