简体   繁体   English

使输入字段在JavaScript中不区分大小写

[英]Make input fields case insensitive in JavaScript

I have a problem with my code, namely: If in input #search_code I introduce letter 'm' and in #insert_code input I introduce letter "M", function returns "is not ok". 我的代码有问题,即:如果在输入#search_code引入字母“ m”,在#insert_code输入中引入字母“ M”,则函数返回“不正常”。 I tried to make uppercase inputs with CSS text-transform: uppercase; 我试图通过CSS text-transform: uppercase;进行大写输入text-transform: uppercase; but it does not work. 但它不起作用。 What can we do to make input fields case insensitive? 如何使输入字段不区分大小写?

 var search_code = document.getElementById('search_code'); var insert_code = document.getElementById('insert_code'); var result = document.getElementById('result'); var button = document.getElementById('button'); var audio = new Audio('sound.wav'); // respond to button click button.onclick = function validate(e) { e.preventDefault(); // show verification result: if (search_code.value == insert_code.value) { result.textContent = 'code ok'; result.className = "ok"; audio.play(); } else { result.textContent = 'code is not ok'; result.className = "not-ok"; } // clear input when wrong: if (search_code.value !== insert_code.value) { insert_code.value = ''; } return false; }; function clearField(input) { input.value = ""; }; $(document).ready(function(){ $('#search_code').bind("cut copy paste",function(e) { e.preventDefault(); }); }); 
 ... <form> <input type="text" name="search_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value=""/><br/> <input type="" name="insert_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="scaneaza codul" autocomplete="off" value=""/><br/><br/> <input type="submit" id="button" name="button" value="verifica COD" /> </form> </div> <div id="result"></div> </div> <script src="js/action_input.js"></script> </body> </html> 

You should be checking the variables for quality by first converting them into either upper or lower case. 首先应将变量转换为大写或小写形式,以检查其质量。 You can use String's "toLowerCase()" before comparisions.(If you need case insensitive comparision) 可以在比较之前使用String的“ toLowerCase()”。(如果需要不区分大小写的比较)

search_code.value.toLowerCase() == insert_code.value.toLowerCase() 

Modify your condition check to as below 将条件检查修改为如下

if (search_code.value.toLowerCase() == insert_code.value.toLowerCase()) {

That should make it run with no issues 那应该使它运行没有问题

Convert those values to be compared to lowercase so that case sensitivity is no longer an issue. 将这些值转换为小写,以便区分大小写不再是问题。

 var search_code = document.getElementById('search_code'); var insert_code = document.getElementById('insert_code'); var result = document.getElementById('result'); var button = document.getElementById('button'); var audio = new Audio('sound.wav'); // respond to button click button.onclick = function validate(e) { e.preventDefault(); // show verification result: if (search_code.value.toLowerCase() == insert_code.value.toLowerCase()) { result.textContent = 'code ok'; result.className = "ok"; audio.play(); } else { result.textContent = 'code is not ok'; result.className = "not-ok"; } // clear input when wrong: if (search_code.value.toLowerCase() !== insert_code.value.toLowerCase()) { insert_code.value = ''; } return false; }; function clearField(input) { input.value = ""; }; $(document).ready(function() { $('#search_code').bind("cut copy paste", function(e) { e.preventDefault(); }); }); 
 <form> <input type="text" name="search_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value="" /><br/> <input type="" name="insert_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="scaneaza codul" autocomplete="off" value="" /><br/><br/> <input type="submit" id="button" name="button" value="verifica COD" /> </form> <div id="result"></div> 

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

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