JavaScript编程笔记
新中……
1、数据类型验证问题
Asp.Net虽然有验证控件,但是有些复杂的验证还是得传到服务器上进行,用js速度和性能都比较好
<script>
//检查是否为任意数(实数)
function isNumeric(strNumber) {
var newPar=/^(-|\+)?\d+(\.\d+)?$/
alert(newPar.test(strNumber)); }
//检查是否为正数
function isUnsignedNumeric(strNumber) {
var newPar=/^\d+(\.\d+)?$/
alert(newPar.test(strNumber)); }
//检查是否为整数
function isInteger(strInteger) {
var newPar=/^(-|\+)?\d+$/
alert(newPar.test(strInteger)); }
//检查是否为正整数
function isUnsignedInteger(strInteger) {
var newPar=/^\d+$/
alert(newPar.test(strInteger)); }
</script>
2、asp.net中用alert()实现换行问题
asp.net中用response.Write("<script>alert('提示:\n操作出错!');</script>");无法执行。
将字符串写成
say="提示:\\n操作出错!"
say=@"提示:\n操作出错!"
3、js实现刷新验证码
单击图片刷新:
<img id="CheckImg" src="CreatImage.aspx" onclick="this.src='CreatImage.aspx?temp='+Math.random()"/>
单击“刷新验证码”按钮刷新
function imagechick()
{
form1.CheckImg.src="CreatImage.aspx?temp="+Math.random();
return false;//不上传到服务器
}
其中CreatImage.aspx为产生验证码图片的页面,源码见:C#产生图形验证码
4、js中使用正则表达式的方法
格式是:/正则表达式/
必须用两个斜杠把正则表达式括起来。
如非法字符检测正则表达式为:^(?:[\u4e00-\u9fa5]*\w*\s*)+$
使用的代码为:
var newPar=/^(?:[\u4e00-\u9fa5]*\w*\s*)+$/;
if(newPar.test("1234&1238*_~"))
{
&
相关文档:
Regular Expression Optimization 正则表达式优化
Incautiously crafted regexes can be a major performance bottleneck (the upcoming section, "Runaway Backtracking" on page 91, contains several examples showing how severe this can be), but there is a lot you can do to improve re ......
A Note on Benchmarking 测试基准说明
Because a regex's performance can be wildly different depending on the text it's applied to, there's no straightforward way to benchmark regexes against each other. For the best result, you need to benchmark your regexes on test strings o ......
String Trimming 字符串修剪
Removing leading and trailing whitespace from a string is a simple but common task. Although ECMAScript 5 adds a native string trim method (and you should therefore start to see this method in upcoming browsers), JavaScript has not historically in ......
Use the Fast Parts 使用速度快的部分
Even though JavaScript is often blamed for being slow, there are parts of the language that are incredibly fast. This should come as no surprise, since JavaScript engines are built in lower-level languages and are therefore compiled. Thou ......
第十章 Tools 工具
Having the right software is essential for identifying bottlenecks in both the loading and running of scripts. A number of browser vendors and large-scale websites have shared techniques and tools to help make the Web faster and more efficient. This ......