只能输入数字
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)'>
<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判断
Javascript
<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 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>
半角判断
Javascript
<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 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>
获得字节数
Javascript
<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 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>
去掉空格
Javascript
<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>
<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 |
Javascript
<input onclick="this.style.imeMode='active'" >
<input onclick="this.style.imeMode='inactive'" >
<input onclick="this.style.imeMode='auto'" >
<input onclick="this.style.imeMode='disabled'" >
<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自动换行
HTML
<TEXTAREA NAME="textarea" ROWS="10" COLS="10" style="overflow-y:hidden"></TEXTAREA>
<TEXTAREA NAME="textarea" ROWS="10" COLS="10" style="overflow-y:hidden"></TEXTAREA>
Textarea最大位数限制
Javascript
<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>
<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>