Home > Web Front-end > JS Tutorial > body text

JavaScript implements text box restriction input strength sharing

巴扎黑
Release: 2018-05-11 11:15:12
Original
1400 people have browsed it

为大家分享的JavaScript限制文本框的输入内容代码如下

<style type="text/css">
<!--
.STYLE1 {
 color: #0099FF;
 font-weight: bold;
 font-size: x-large;
}
-->
</style>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p align="center" class="STYLE1">亲,试试效果吧!</p>
<p align="center"><strong>请输入数字或字母</strong>:
 <input onkeyup="value=value.replace(/[W]/g,"") "onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^d]/g,""))">
</p>
<p align="center"><strong>请输入数字,只能输入数字哦</strong>:
 <input onkeyup="value=value.replace(/[^d]/g,"") "onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^d]/g,""))">
</p>
<p align="center"><strong>请输入汉字,只能输入汉字哦</strong>:
 <input onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,"")" onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^u4E00-u9FA5]/g,""))">
</p>
Copy after login

运行效果图:

下面再给大家补充一些:

小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:

<
input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || //./d/d$/.test(value))event.returnValue=false">
Copy after login

文本框只能输入数字代码(小数点也不能输入)

<
input onkeyup="this.value=this.value.replace(//D/g,"")" onafterpaste="this.value=this.value.replace(//D/g,"")">
Copy after login

其实还可以限制其他内容,比如只能是字母,大家可以尝试一些其他的。

The above is the detailed content of JavaScript implements text box restriction input strength sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!