JavaScript limit input script Daquan _javascript tips

Source: Internet
Author: User
Tags date1 getdate microsoft frontpage
1. Can only enter the Chinese characters
<input onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') ">

2. Only numbers can be entered:
<input onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') ">
Simple prohibition of entering Chinese characters
<input type= "text" style= "ime-mode:disabled" >
Enter numbers and decimal points:
Onkeyup= "Value=value.replace (/[^\d{1,}\.\d{1,}|\d{1,}]/g,") "
JavaScript can only enter numbers and ":". 2007-11-24 15:50<input type=text id= "Aa1" onkeyup= this.value=this.value.replace (/[^\d &:]/g, "onblur=" This.value=this.value.replace (/[^\d&:]/g, ') "onafterpaste=" this.value= This.value.replace (/[^\d&:]/g, ') "/>
Only digits and ":", for example, can be used when entering the time.
<input type=text id= "AA" onkeyup= "Value=value.replace (/[^\w&=]|_/ig, '") "onblur=" Value=value.replace (/[^\w &=]|_/ig, ")"/>
You can only enter letters and equal signs, and you cannot enter Chinese characters.
Other things:
Scripts that can only enter numbers javascript--
1.<input onkeyup= "This.value=this.value.replace (/\d/g,") "
Onafterpaste= "This.value=this.value.replace (/\d/g, ')" >
The upper half means that the keyboard can only be typed as a number, and the second sentence is a paste and only a number.
2.<input name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' can only enter numbers '); this.value= ';} ' >
3.<input onkeyup= "if (isNaN (value)) ExecCommand (' Undo ') ' onafterpaste= ' if (isNaN (value)) ExecCommand (' Undo ') >
JavaScript limits can only be entered in numbers and English--
function Isregname (checkobj)
{
var Checkok = "0123456789-_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
var checkstr = checkobj;
var allvalid = true;
var decpoints = 0;

for (i = 0; i < checkstr.length; i++)
{
ch = checkstr.charat (i);
for (j = 0; J < Checkok.length; J + +)
if (ch = = Checkok.charat (j))
Break
if (j = = Checkok.length)
{
Allvalid = false;
Break
}
}
Return (Allvalid)
}
----------------
if (!) ( Isregname (Obj.loginname.value))) {
Alert ("[member code] does not conform to the specification, the member code can only be English letters or numbers");
Obj.loginname.focus ();
return (false);
}
if (!) ( Isregname (Obj.password.value))) {
Alert ("[Password] is not in accordance with the specification, the password can only be English letters or numbers");
Obj.password.focus ();
return (false);
}

4.javascript input box can only be entered in English and digital
<input onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') ">
5. You can use JavaScript to check the text box to filter out non-0-9 characters.
<script language= "javascript" event= "onkeydown" for= "Document" >
if (event.srcelement.name== ' TextBox1 ')
{
if (! Keyisnumber (Event.keycode))
{
Return false;//This sentence is the most critical
}
}
</script>
<script language= "JavaScript" >
function Keyisnumber (keycode)
{
If the character entered is between 0-9, or the backspace, Del key
if ((keycode>47) && (keycode<58) | | (keycode==8) | | (keycode==46))
{
return true;
}
Else
{
return false;
}
}
</script>[url=http://blog.csdn.net/xujh/admin/editposts.aspx][/url]


6. Restrict to text box only enter IP address format
<HTML>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<meta name= "generator" content= "Microsoft FrontPage 4.0" >
<meta name= "ProgId" content= "FrontPage.Editor.Document" >
<style>
. A3{width:30;border:0;text-align:center}
</style>
<script>
function Mask (obj) {
Obj.value=obj.value.replace (/[^\d]/g, "")
Key1=event.keycode
if (key1==37 | | key1==39)
{Obj.blur ();
Nextip=parseint (Obj.name.substr (2,1))
nextip=key1==37?nextip-1:nextip+1;
Nextip=nextip>=5?1:nextip
Nextip=nextip<=0?4:nextip
eval ("IP" +nextip+ ". Focus ())"
}
if (obj.value.length>=3)
if (parseint (obj.value) >=256 | | parseint (obj.value) <=0)
{
Alert (parseint (obj.value) + "IP address error! ")
Obj.value= ""
Obj.focus ()
return false;
}
Else
{Obj.blur ();
Nextip=parseint (Obj.name.substr (2,1)) +1
Nextip=nextip>=5?1:nextip
Nextip=nextip<=0?4:nextip
eval ("IP" +nextip+ ". Focus ())"
}
}
function Mask_c (obj)
{
Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')
}

</script>
&LT;TITLE&GT;IP Address Input </title>

&LT;BODY&GT;IP Address input
<div style= "Border-width:1;border-color:balck;border-style:solid;width:165;font-size:9pt" >
<input type=text name=ip1 maxlength=3 class=a3 onkeyup= "Mask (This)" Onbeforepaste=mask_c ().
<input type=text name=ip2 maxlength=3 class=a3 onkeyup= "Mask (This)" Onbeforepaste=mask_c ().
<input type=text name=ip3 maxlength=3 class=a3 onkeyup= "Mask (This)" Onbeforepaste=mask_c ().
<input type=text name=ip4 maxlength=3 class=a3 onkeyup= "Mask (This)" Onbeforepaste=mask_c () >
</div>
</body>
</HTML>


7. Limit the date control of the input format
  
-----Date control Another way of thinking, restricting the input format of the date control-----about date input, now online a lot of date controls, here, I try to imitate the part of the CS program in the date input control, from another angle to consider the date format input, that is, enter the date of the fixed format, Users can enter data only where the program requires it.
Routine as follows, I hope to help you ~ ~ ~ (time haste, routine is IE only)
<script language= ' JavaScript ' >
/**//*
* Added by Liuxiaochong 2005.4.25
* Restrict the entered date control
* Param:txtname the name of the text box for which you want to restrict input
*
* Function Description: 1, can only enter the number
* 2, around key can move edit focus
* 3, up and down keys can fine-tune the data
* 4, the control contains a legal checksum of the date
*/
function Regdatecontrol (txtname)
{
var oinput = document.getElementById (txtname);
Oinput.middlechar = '-';
Oinput.selectindex = 1; Select middle Age By default
Oinput.maxlength = 10;
OInput.style.imeMode = ' disabled ';
Oinput.value = Specialtext_getdate (Oinput.middlechar);
Oinput.charwidth = Oinput.createtextrange (). Boundingwidth/oinput.maxlength;
Registering a Click event
Oinput.onclick = specialtext_clickevent;
Oinput.onkeydown = specialtext_keydownevent;
Oinput.onfocus = function () {specialtext_selectyear (this);}
Oinput.onblur = function ()
{
Specialtext_validyear (this);
Specialtext_validmonth (this);
Specialtext_validdate (this);
}
Shielding the right mouse button and dragging operations
Oinput.oncontextmenu = function () {return false;}
Oinput.ondrop = function () {return false;}
}
Mouse clicks, depending on the location of the date of the split selection
function Specialtext_clickevent ()
{
Event.cancelbubble = true;
Specialtext_validyear (this);
Specialtext_validmonth (this);
Specialtext_validdate (this);
if (Event.offsetx <= specialtext_getcharwidth (this.charwidth,4))
Specialtext_selectyear (this);
else if (Event.offsetx > Specialtext_getcharwidth (this.charwidth,4)
&& event.offsetx <= specialtext_getcharwidth (this.charwidth,7))
Specialtext_selectmonth (this);
else if (Event.offsetx > Specialtext_getcharwidth (this.charwidth,7))
Specialtext_selectdate (this);
}
Selected year
function Specialtext_selectyear (oinput)
{
var oRange = Oinput.createtextrange ();
Orange.movestart (' character ', 0);
Orange.moveend (' character ',-6);
The representative selected the year
Oinput.selectindex = 1;
Orange.select ();
}
Month selected
function Specialtext_selectmonth (oinput)
{
var oRange = Oinput.createtextrange ();
Orange.movestart (' character ', 5);
Orange.moveend (' character ',-3);
The representative selected the month
Oinput.selectindex = 2;
Orange.select ();
}
Selected date
function Specialtext_selectdate (oinput)
{
var oRange = Oinput.createtextrange ();
Orange.movestart (' character ', 8);
The date is selected by the representative
Oinput.selectindex = 3;
Orange.select ();
}
Verify year validity
function Specialtext_validyear (oinput)
{
var arrvalue = OInput.value.split (Oinput.middlechar);
var stryear = arrvalue[0];
if (parseint (stryear,10) = = 0)
Arrvalue[0] = 2000;
If the year is less than 4 bits, increase on 2000 basis
else if (Stryear.length < 4)
ARRVALUE[0] = Watts + parseint (stryear,10);
Oinput.value = Arrvalue.join (Oinput.middlechar);
}
Verify Month validity
function Specialtext_validmonth (oinput)
{
var arrvalue = OInput.value.split (Oinput.middlechar);
var strmonth = arrvalue[1];
If 0 is entered in the month, it will be processed by January
if (parseint (strmonth,10) = = 0)
ARRVALUE[1] = ' 01 ';
If the month is one, the front is 0
else if (Strmonth.length < 2)
ARRVALUE[1] = ' 0 ' + strmonth;
If the month is greater than December, automatically converted to December
else if (parseint (strmonth,10) > 12)
ARRVALUE[1] = ' 12 ';
Oinput.value = Arrvalue.join (Oinput.middlechar);
}
Verify Date Validity
function Specialtext_validdate (oinput)
{
var arrvalue = OInput.value.split (Oinput.middlechar);
var stryear = arrvalue[0];
var strmonth = arrvalue[1];
var strdate = arrvalue[2];
var intmonth = parseint (strmonth,10);
if (parseint (strdate,10) = = 0)
ARRVALUE[2] = ' 01 ';
else if (Strdate.length < 2)
ARRVALUE[2] = ' 0 ' + strdate;
Else
{
If the maximum number of days of the month is exceeded, the maximum number of days is placed
var monthmaxdates = specialtext_getmonthdates (stryear,strmonth);
if (parseint (strdate,10) > Monthmaxdates)
ARRVALUE[2] = monthmaxdates;
}
Oinput.value = Arrvalue.join (Oinput.middlechar);
}
function Specialtext_yearadd (Oinput,isminus)
{
var arrvalue = OInput.value.split (Oinput.middlechar);
var stryear = arrvalue[0];
if (Isminus)
{
Arrvalue[0] = parseint (stryear,10)-1;
if (parseint (arrvalue[0],10) < 1)
Arrvalue[0] = ' 0001 ';
}
Else
Arrvalue[0] = parseint (stryear,10) + 1;
Oinput.value = Arrvalue.join (Oinput.middlechar);
Specialtext_validyear (Oinput);
Specialtext_selectyear (Oinput);
}
function Specialtext_monthadd (Oinput,isminus)
{
var arrvalue = OInput.value.split (Oinput.middlechar);
var strmonth = arrvalue[1];
if (Isminus)
{
ARRVALUE[1] = parseint (strmonth,10)-1;
if (parseint (arrvalue[1],10) = = 0)
ARRVALUE[1] = ' 12 ';
}
Else
{
ARRVALUE[1] = parseint (strmonth,10) + 1;
if (parseint (arrvalue[1],10) = = 13)
ARRVALUE[1] = ' 01 ';
}
Oinput.value = Arrvalue.join (Oinput.middlechar);
Specialtext_validmonth (Oinput);
Specialtext_selectmonth (Oinput);
}
function Specialtext_dateadd (Oinput,isminus)
{
var arrvalue = OInput.value.split (Oinput.middlechar);
var stryear = arrvalue[0];
var strmonth = arrvalue[1];
var strdate = arrvalue[2];
var monthmaxdates = specialtext_getmonthdates (stryear,strmonth);
if (Isminus)
{
ARRVALUE[2] = parseint (strdate,10)-1;
if (parseint (arrvalue[2],10) = = 0)
ARRVALUE[2] = monthmaxdates;
}
Else
{
ARRVALUE[2] = parseint (strdate,10) + 1;
if (parseint (arrvalue[2],10) = = (monthmaxdates + 1))
ARRVALUE[2] = ' 01 ';
}
Oinput.value = Arrvalue.join (Oinput.middlechar);
Specialtext_validdate (Oinput);
Specialtext_selectdate (Oinput);
}
function Specialtext_keydownevent ()
{
If you press the number key
if ((Event.keycode >= && event.keycode <= 57) | |
(Event.keycode >= && event.keycode <= 105))
{
var oRange = Document.selection.createRange ();
if (ORange.text.indexOf (This.middlechar)!=-1)
Event.returnvalue = false;
Else
Event.returnvalue = true;
}
If you press the arrow keys
else if (event.keycode >= && event.keycode <= 40)
{
Event.returnvalue = false;
var keycode = Event.keycode;
Press the left button
if (keycode = 37)
{
if (This.selectindex = 1)
{
Specialtext_validyear (this);
Specialtext_selectdate (this);
}
else if (This.selectindex = 2)
{
Specialtext_validmonth (this);
Specialtext_selectyear (this);
}
else if (This.selectindex = 3)
{
Specialtext_validdate (this);
Specialtext_selectmonth (this);
}
}
Press the right key
if (keycode = 39)
{
if (This.selectindex = 1)
{
Specialtext_validyear (this);
Specialtext_selectmonth (this);
}
else if (This.selectindex = 2)
{
Specialtext_validmonth (this);
Specialtext_selectdate (this);
}
else if (This.selectindex = 3)
{
Specialtext_validdate (this);
Specialtext_selectyear (this);
}
}
Pressed the UP key
if (keycode = 38)
{
if (This.selectindex = 1)
{
Specialtext_validyear (this);
Specialtext_yearadd (this,true);
}
else if (This.selectindex = 2)
{
Specialtext_validmonth (this);
Specialtext_monthadd (this,true);
}
else if (This.selectindex = 3)
{
Specialtext_validdate (this);
Specialtext_dateadd (this,true);
}
}
Pressed the next key
if (keycode = 40)
{
if (This.selectindex = 1)
{
Specialtext_validyear (this);
Specialtext_yearadd (This,false);
}
else if (This.selectindex = 2)
{
Specialtext_validmonth (this);
Specialtext_monthadd (This,false);
}
else if (This.selectindex = 3)
{
Specialtext_validdate (this);
Specialtext_dateadd (This,false);
}
}
}
If you press F5 or TAB, do not block
else if (Event.keycode = 116 | | event.keycode = 9)
Event.returnvalue = true;
Else
{
Event.returnvalue = false;
Event.keycode = 0;
}
}
/**//*---------------------Auxiliary Function-----------------------* *
Get the default date
function Specialtext_getdate (Middlechar)
{
var odate = new Date ();
return odate.getyear () + Middlechar
+ (Odate.getmonth () < 10? (' 0 ' + odate.getmonth ()): Odate.getmonth ()) + Middlechar
+ (Odate.getdate () < 10? (' 0 ' + odate.getdate ()): Odate.getdate ());
}
Get character pixel width
function Specialtext_getcharwidth (charwidth,charnum)
{
return charNum * charwidth;
}
Get the maximum number of days of the month of a year
function Specialtext_getmonthdates (stryear,strmonth)
{
var intmonth = parseint (strmonth,10);
if (intmonth = 1 | | intmonth = 3 | | | intmonth = 5 | | intmonth = = 7
|| Intmonth = 8 | | Intmonth = 10 | | Intmonth = 12)
return 31;
Process a 30-day month
else if (intmonth = 4 | | intmonth = 6 | | intmonth = 9 | | intmonth = 11)
return 30;
Handling February
Else
{
Leap
if (Specialtext_isleapyear (stryear))
return 29;
Excepting
Else
return 28;
}
}
Judge whether it is a leap year
function Specialtext_isleapyear (stryear)
{
var intyear = parseint (stryear,10);
if (intyear% 4 = 0 && intyear% 100!= 0) | |
(intyear% = 0 && intyear% 400 = 0))
return true;
Else
return false;
}
/**//*----------------------------can be placed in the external JS dateinputcontrol.js--------------------* *
function init ()
{
Regdatecontrol (' date1 ');
}
</SCRIPT>
<body onload= ' init () ' >
<input type= ' text ' name= ' date1 ' >
</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.