关于javascript的问题 问题1:如何判断上传图片的大小。我要判断图片的大小如果超过2M就给出提示;问题2:求一个验证网址URL的正则表达式;在此先谢谢各位 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 问题2:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? 问题1:<script language="JavaScript" type="text/JavaScript">var img=null;function checkPic(picForm){ var location=picForm.pic.value; imgMaxSize=2*1024*1024; //图片文件最大多少K ,自己设置 if(location==""){ alert("请先选择图片文件"); return false; } var point = location.lastIndexOf("."); var type = location.substr(point); if(type==".jpg"||type==".gif"||type==".JPG"||type==".GIF"){ //控制输入的图片格式 img=document.createElement("img"); img.src=location; alert("图片尺寸为:"+img.fileSize); if(img.fileSize>imgMaxSize){ alert("图片尺寸请不要大于2M"); return false; }else return true; } else{ alert("只能输入jpg或者gif格式的图片"); return false; } return false; }function changesrc(){yourpic.src=picForm.pic.value;}</script><table width="100%" border="0"> <tr> <td> <div align="center"><img border="1" id="yourpic" onload="javascript:if(this.width>175)this.width=175;if(this.height>150)this.height=150;"> </div> </td> </tr> <form name="picForm" action="#" method="post" enctype="multipart/form-data" onsubmit="return checkPic(picForm);"> <tr> <td> <div align="center"><input type="file" name="file" size="10" id="pic" onchange="changesrc()"/></div> </td> </tr> <tr> <td> <div align="center"><input type="submit" value="上传照片" /></div> </td> </tr></table>问题2:1)简单的:匹配网址URL的正则表达式:[a-zA-z]+://[^s]*2)全面的:<script language="JavaScript"><!-- function IsURL(str_url){ var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 + "|" // 允许IP和DOMAIN(域名) + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,4})?" // 端口- :80 + "((/?)|" // a slash isn't required if there is no file name + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; var re=new RegExp(strRegex); //re.test() if (re.test(str_url)){ return (true); }else{ return (false); } }--></script> 问 lcz_2;我试过了。不行呀。难道是我写错了。我就用的你给出的代码。在输入"http://adb".提示ok这是?? 输入"http://adb",我试了,return (false); 没问题啊.你自己再检查下你调用function IsURL(str_url)时,str_url传值是否正确? 哦。谢谢了。是我js其他地方有错。导致判断url函数调用不成功。 <script type="text/javascript">show(7);show(-1);function show(p){alert(2<p<6);}</script>//请问以上js代码会输出的结果是? 帮个忙,谁能把Javascript代码写成JQuery的代码 自动修改浏览器查看里的编码为中文,怎么做? javascript的闭包与变量 json问题求急. js如何获取边框宽度 急!急!急!高分求救一个关于JavaScript的问题(100分!) text的value为什么不能保留呢。 求一简单程序 不做超级连接,如何使图片鼠标移上,鼠标就变成手形? 如何使下拉列表可以写进去新值? 如何把js的参数传递给JSP,两个参数的。 JavaScript与服务器端交互
<script language="JavaScript" type="text/JavaScript">
var img=null;
function checkPic(picForm){
var location=picForm.pic.value;
imgMaxSize=2*1024*1024; //图片文件最大多少K ,自己设置
if(location==""){
alert("请先选择图片文件");
return false;
}
var point = location.lastIndexOf(".");
var type = location.substr(point);
if(type==".jpg"||type==".gif"||type==".JPG"||type==".GIF"){ //控制输入的图片格式
img=document.createElement("img");
img.src=location;
alert("图片尺寸为:"+img.fileSize);
if(img.fileSize>imgMaxSize){
alert("图片尺寸请不要大于2M");
return false;
}else
return true;
}
else{
alert("只能输入jpg或者gif格式的图片");
return false;
}
return false;
}
function changesrc(){
yourpic.src=picForm.pic.value;
}
</script>
<table width="100%" border="0">
<tr>
<td>
<div align="center"><img border="1" id="yourpic"
onload="javascript:if(this.width>175)this.width=175;if(this.height>150)this.height=150;">
</div>
</td>
</tr>
<form name="picForm" action="#" method="post"
enctype="multipart/form-data" onsubmit="return checkPic(picForm);">
<tr>
<td>
<div align="center"><input type="file" name="file" size="10"
id="pic" onchange="changesrc()"/></div>
</td>
</tr>
<tr>
<td>
<div align="center"><input type="submit" value="上传照片" /></div>
</td>
</tr>
</table>问题2:
1)简单的:匹配网址URL的正则表达式:[a-zA-z]+://[^s]*
2)全面的:
<script language="JavaScript">
<!--
function IsURL(str_url){
var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var re=new RegExp(strRegex);
//re.test()
if (re.test(str_url)){
return (true);
}else{
return (false);
}
}
-->
</script>
这是??
你自己再检查下你调用function IsURL(str_url)时,str_url传值是否正确?
show(7);show(-1);
function show(p)
{
alert(2<p<6);}</script>
//请问以上js代码会输出的结果是?