【Javascript】关于TextBox代码整理
和TextBox有关的Javascript代码整理
只能输入数字


<script>
function keypress(e)
{
if ([e.keyCode||e.which]==8) //this is to allow backspace
return true;
if ([e.keyCode||e.which] < 48 || [e.keyCode||e.which] > 57)
e.preventDefault? e.preventDefault() : e.returnValue = false;
}
</script>
<input type="text" name="textbox1" id="textbox1" onKeypress='keypress(event)'>
EMail判断


<script language=javascript>
function isEmailFormatValid(emailSrc) {
var email = emailSrc.replace(/^\s+|\s+$/g, '');
if (email == '') {
return false;
}
var regex = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (regex.test(email)) {
var str = " !#$%^&*()+=|\{[]}:;'<,>?/" ;
var str2 = '"';
var result = true;
for (var j = 0; j < email.length; j++) {
if (str.indexOf(email.charAt(j)) != -1 || str2.indexOf(email.charAt(j)) != -1) {
result = false;
break;
}
}
if (result == true) {
return true;
}
else {
return false;
}
} else {
return false;
}
}
function btn2_Click() {
var strTest;
if (isEmailFormatValid(document.all['TextBox7'].value) == true)
{
strTest = 'It is ok'
} else {
strTest = 'It is failed'
}
alert(strTest);
}
</script>
半角判断


<script language=javascript>
function checkNarrow(character) {
for (var i = 0; i < character.length; i++) {
if (escape(character.charAt(i)).length >= 4 ) {
return false;
}
}
return true;
}
function btn1_Click() {
var strTest;
if (checkNarrow(document.all['TextBox6'].value) == true)
{
strTest = 'It is ok'
} else {
strTest = 'It is failed'
}
alert(strTest);
}
</script>
获得字节数


<script language=javascript>
function getLengthByte(varField)
{
var i=0;
var j=0;
for (var i = 0; i < varField.length; i++) {
if ((varField.charCodeAt(i) > 0 && varField.charCodeAt(i) < 127 )
|| (varField.charCodeAt(i) > 65376 && varField.charCodeAt(i) < 65440)){
j=j+1;
}
else {
j=j+2
}
}
return j;
}
</script>
去掉空格


<script language=javascript>
function trimX(strValue) {
strValue = strValue.replace(/^\s+|\s+$/g, '');
strValue = strValue.replace(/^\u3000+|\u3000+$/g, '');
return strValue;
}
function rTrimX(strValue) {
strValue = strValue.replace(/\s+$/g, '');
strValue = strValue.replace(/\u3000+$/g, '');
return strValue;
}
function rTrimX(strValue) {
strValue = strValue.replace(/^\s+/g, '');
strValue = strValue.replace(/^\u3000+/g, '');
return strValue;
}
function btn1_Click() {
var strTest = "inStr: ";
strTest = strTest + "\n";
strTest = strTest + '#' + document.all['TextBox5'].value + '#';
strTest = strTest + "\n\n";
strTest = strTest + "outStr: "
strTest = strTest + "\n";
strTest = strTest + '#' + trimX(document.all['TextBox5'].value) + '#';
alert(strTest);
}
</script>
输入法控制
active | |
inactive | |
auto | |
disable |


<input onclick="this.style.imeMode='active'" >
<input onclick="this.style.imeMode='inactive'" >
<input onclick="this.style.imeMode='auto'" >
<input onclick="this.style.imeMode='disabled'" >
Textarea自动换行


<TEXTAREA NAME="textarea" ROWS="10" COLS="10" style="overflow-y:hidden"></TEXTAREA>
Textarea最大位数限制


<script language=javascript>
function doKeypress(control,maxLength)
{
var element=control
if(!isNaN(maxLength))
{
maxLength = parseInt(maxLength)
var oTR = element.document.selection.createRange()
if(oTR.text.length >= 1)
event.returnValue = true
else if(element.value.length > maxLength-1)
event.returnValue = false
}
}
function doKeydown(control,maxLength)
{
var element=control
setTimeout(function()
{
maxLength = parseInt(maxLength)
if(!isNaN(maxLength))
{
if(element.value.length > maxLength-1)
{
var oTR = window.document.selection.createRange()
oTR.moveStart("character", -1*(element.value.length-maxLength))
oTR.text = ""
}
}
},1)
}
function doBeforePaste(control,maxLength)
{
if(!isNaN(maxLength))
event.returnValue = false
}
function doPaste(control,maxLength)
{
var element=control
if(!isNaN(maxLength))
{
event.returnValue = false
maxLength = parseInt(maxLength)
var oTR = element.document.selection.createRange()
var iInsertLength = maxLength - element.value.length + oTR.text.length
var sData = window.clipboardData.getData("Text").substr(0, iInsertLength)
oTR.text = sData;
}
}
</script>
<textarea rows="4" cols="30" id="testarea" name="testarea"
onkeypress="doKeypress(this,10)"
onkeydown="doKeydown(this,10)"
onbeforepaste="doBeforePaste(this,10)"
onpaste="doPaste(this,10)"
></textarea>
分类:
JavaScript
标签:
Textbox
随笔分类 (333)
Cnblogs's
Front End
Oracle's
Software's
- Apache HTTP Server
- CodeSmith Community
- Grapecity(FAQ)
- Mybase
- ClubFarPoint(Forum)
- Beyond Compare
- CrystalReport(FAQ)
- 秀丸
- Software Advice
- Software: Business & Nonprofit | Reviews and Top Software at Capterra
- Capterra
- Business Software Reviews from Software Advice
Copyright © 2025 sekihin
Powered by .NET 9.0 on Kubernetes
Powered by .NET 9.0 on Kubernetes
![]() | 本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2007-01-10 注册Active Reports 去除红线
2007-01-10 [转].NET面试题集
2007-01-10 利用Visual Basic命令操作文件
2007-01-10 随机文件文件的操作