Home  >  Article  >  Web Front-end  >  Only numbers are allowed to be entered in the input box (positive and negative with decimals), and when entering positive numbers, the numbers are red and negative numbers are green_html/css_WEB-ITnose

Only numbers are allowed to be entered in the input box (positive and negative with decimals), and when entering positive numbers, the numbers are red and negative numbers are green_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:57:132789browse

Thinking of using onkeyup or some other method to achieve it. Ask God for answers.


Reply to discussion (solution)

This involves js.
For events, just use onkeyup of the keyboard key event.
When the button is lifted, a judgment is made in the event processing function: whether the first character in the input box is "-", if the value of the color attribute of the style attribute of the input element is modified, it is green, otherwise it is red. . If it is jQuery, just set the .css() method
and it only needs to be judged once during the continuous input process.

This involves js.
For events, just use onkeyup of the keyboard key event.
When the button is lifted, a judgment is made in the event processing function: whether the first character in the input box is "-", if the value of the color attribute of the style attribute of the input element is modified, it is green, otherwise it is red. . If it is jQuery, just set the .css() method
and it only needs to be judged once during the continuous input process.



Hello, I am not familiar with js, please take a look again. It’s still wrong for me to write like this
function check(){
var a = document.FormName.tit.value;
if(a.indexOf("-")>=0){
document.getElementById("tit").style.color = "width:500px;color:green";
}else if(a.indexOf(" ")>=0){
document.getElementById( "tit").style.color = "width:500px;color:red";
}
}

This involves js.
For events, just use onkeyup of the keyboard key event.
When the button is lifted, a judgment is made in the event processing function: whether the first character in the input box is "-", if the value of the color attribute of the style attribute of the input element is modified, it is green, otherwise it is red. . If it is jQuery, just set the .css() method
and it only needs to be judged once during the continuous input process.




Oops. It turned out to be better if I changed it myself. Thank you, brother on the 2nd floor~ I will send you the post
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn