我正在尝试使用javascript调用文本输入

时间:2017-03-30 08:39:04

标签: javascript html5 validation input

我正在检查以确保表单已完成,这是我的一段代码 检查密码

function checkpass()
{
   
    var pass1 = document.getElementById('pass1');
    var pass2 = document.getElementById('pass2');
   
    var message = document.getElementById('confirmMessage');
  
    var goodColor = "#66cc66";
    var badColor = "#ff6666";
  
    if(pass1.value == pass2.value){
        
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "Passwords Match!"
    }else{
        
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "Passwords Do Not Match!"
    }
}  
<label >Password:</label>
            <input type="password" name="pass1" id="pass1">
      <p></p>
      
        <label >Verify Password:</label>
        <input type="password" name="pass2" id="pass2" onBlur="checkpass()">
<span id="confirmMessage" class="confirmMessage"></span>

这样可行,但是当我尝试使用名称输入执行类似操作时,它无法正常工作

 <input type="text" name="name1" id="name1" onblur="checkname">
 <label>First Name</label> 
function checkname(){var name1 = document.getElementById('name1');if (name1 == null) {alert('entername');}}

如果你能告诉我一个简单的错误让我看起来像个白痴会很棒,或者如果你能告诉我一个更好的方法去完成整个事情也会很棒。 我们非常感谢您提供的任何帮助。

4 个答案:

答案 0 :(得分:0)

您需要检查空名称而不是空名称,请尝试:

var name1 = document.getElementById('name1').value;
if(name1.trim() == "")

您还需要在onblur上调用checkname()函数代替checkname

function checkname(){
var name1 = document.getElementById('name1').value;
if (name1.trim() == "")
{
alert('entername');
}
}
function checkpass()
{
   
    var pass1 = document.getElementById('pass1');
    var pass2 = document.getElementById('pass2');
   
    var message = document.getElementById('confirmMessage');
  
    var goodColor = "#66cc66";
    var badColor = "#ff6666";
  
    if(pass1.value == pass2.value){
        
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "Passwords Match!"
    }else{
        
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "Passwords Do Not Match!"
    }
} 
 
<label >Password:</label>
            <input type="password" name="pass1" id="pass1">
<br/>
      
        <label >Verify Password:</label>
        <input type="password" name="pass2" id="pass2" onBlur="checkpass()">
        <br/>
<span id="confirmMessage" class="confirmMessage"></span>
<br/>

 <input type="text" name="name1" id="name1" onblur="checkname();">
 <label>First Name</label> 
 <br/>
 <input type="text" name="name2" id="name2">
 <label>Last Name</label> 

答案 1 :(得分:0)

var name1 = document.getElementById('name1')

你忘记了最后的.value

var name1 = document.getElementById('name1').value

答案 2 :(得分:0)

试试这个

<input type="text" name="name1" id="name1" onblur="checkname">
<label>First Name</label> 

function checkname()
{
     var name1 = document.getElementById('name1').value;
     if (name1 == null || name1 == "") 
     {   
        alert('entername');
     }
}

使用ID

选择属性时,您会错过该值

答案 3 :(得分:0)

你写错了

onblur="checkname"

应该如下

onblur="checkname()"

&#13;
&#13;
function checkname(){
  var name1 = document.getElementById('name1');
 
    console.log('name1');
  }
&#13;
<input type="text" name="name1" id="name1" onblur="checkname()">
 <label>First Name</label> 
&#13;
&#13;
&#13;