JavaScript的Decode Code和IP驗證

來源:互聯網
上載者:User

使用JavaScript對資料進行編碼和解碼動作,代碼如下:

 

<mce:script type="text/javascript"><!--<br />var Url = {</p><p>// public method for url encoding<br />encode : function (string) {<br />return escape(this._utf8_encode(string));<br />},</p><p>// public method for url decoding<br />decode : function (string) {<br />return this._utf8_decode(unescape(string));<br />},</p><p>// private method for UTF-8 encoding<br />_utf8_encode : function (string) {<br />string = string.replace(//r/n/g,"/n");<br />var utftext = "";</p><p>for (var n = 0; n < string.length; n++) {</p><p>var c = string.charCodeAt(n);</p><p>if (c < 128) {<br />utftext += String.fromCharCode(c);<br />}<br />else if((c > 127) && (c < 2048)) {<br />utftext += String.fromCharCode((c >> 6) | 192);<br />utftext += String.fromCharCode((c & 63) | 128);<br />}<br />else {<br />utftext += String.fromCharCode((c >> 12) | 224);<br />utftext += String.fromCharCode(((c >> 6) & 63) | 128);<br />utftext += String.fromCharCode((c & 63) | 128);<br />}</p><p>}</p><p>return utftext;<br />},</p><p>// private method for UTF-8 decoding<br />_utf8_decode : function (utftext) {<br />var string = "";<br />var i = 0;<br />var c = c1 = c2 = 0;</p><p>while ( i < utftext.length ) {</p><p>c = utftext.charCodeAt(i);</p><p>if (c < 128) {<br />string += String.fromCharCode(c);<br />i++;<br />}<br />else if((c > 191) && (c < 224)) {<br />c2 = utftext.charCodeAt(i+1);<br />string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));<br />i += 2;<br />}<br />else {<br />c2 = utftext.charCodeAt(i+1);<br />c3 = utftext.charCodeAt(i+2);<br />string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));<br />i += 3;<br />}</p><p>}</p><p>return string;<br />}</p><p>}</p><p>var sss = "http://192.168.5.194/html/base/base_qry_main.php?new=1&time[0][0]=+&time[0][1]=%3E%3D&time[0][2]=12&time[0][3]=29&time[0][4]=2010&time[0][5]=&time[0][6]=&time[0][7]=&time[0][8]=+&time[0][9]=+&submit=Query+DB&num_result_rows=-1&time_cnt=1";</p><p>alert(Url.decode(sss));<br />//alert(sss.replace("&amp;","&","ig"));<br />//alert(Url.decode(sss.replace("&amp;","&","ig")));<br />// --></mce:script>

 

使用JavaScript對IP地址進行校正的代碼如下:

<mce:script type="text/javascript"><!--<br />String.prototype.isIP = function(){<br /> var reSpaceCheck = /^([0-9]{1,3})/.([0-9]{1,3})/.([0-9]{1,3})/.([0-9]{1,3})$/i;<br /> if (reSpaceCheck.test(this)){<br /> this.match(reSpaceCheck);<br /> if (RegExp.$1 <= 255 && RegExp.$1 > 0<br /> && RegExp.$2 <= 255 && RegExp.$2 >= 0<br /> && RegExp.$3 <= 255 && RegExp.$3 >= 0<br /> && RegExp.$4 <= 255 && RegExp.$4 >= 0) {<br /> return true;<br /> } else {<br /> return false;<br /> }<br /> } else {<br /> return false;<br /> }<br />}<br />var strip = "0.0.5.194";<br />alert(strip.isIP());<br />// --></mce:script>

 

使用JavaScript代碼進行清除空格動作的JavaScript代碼如下:

String.prototype.Trim = function()
{
  return this.replace(/(^/s*)|(/s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^/s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(/s*$)/g, "");
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.