经典论坛's Archiver

wanghwei1 发表于 2008-8-28 13:53

急求一JS代码!

高手大侠们:小弟初学JS,想实现一个登录时用的验证功能:验证输入的必须为数字,而且这个数字要大于100小于200,如果不成功输出错误信息,如果成功页面自动跳转到另一个页面.小弟在这里坐等了,我已经研究了很长时间了,期望高手们指点.

wanghwei1 发表于 2008-8-28 14:14

我自己写的,本地通过,在服务器上不行.

我学VB的,用VBS写了一个,在本地行,但传到服务器上就不行了,大家帮忙看看!
<script  language="vbs">   
  function check()   
      Dim a
      a=form1.T1.value
          <!--b=form1.T2.value  -->  
      if   IsNumeric(a)   then   
      a=clng(a)   
        if   a>=0800000   and   a<=0803000   then   
            <!--window.location.href="riyubm.html" -->
                        <!--alert("恭喜您登录成功,请填写您的详细资料!")  -->
            location.href="visa.html"
        else   
            alert("您输入的密码有误!")   
        end   if   
      else   
        alert("您输入的密码有误!")   
      end   if   
  end   function     
</script>
<form id="form1" name="form1" method="post" action="">
  <table width="270" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000033" style="padding:5px">
    <tr>
      <td align="center" bgcolor="#0066CC">报名序号:</td>
      <td bgcolor="#0066CC"><input size=20 name=T1 onchange="check()"></td>
    </tr>
    <tr>
      <td bgcolor="#0066CC">&nbsp;</td>
      <td bgcolor="#0066CC"><label>
        <input type="submit" name="button" id="button" value="下一步" onclick="check()"/>
      &nbsp;&nbsp;&nbsp;</label></td>
    </tr>  
  </table>
</form>

joelioa 发表于 2008-8-28 14:31

[html]
<script>
        function check()
        {
                var s = document.form1.T1.value;
                if( s < 100 || s > 200 )
                {
                alert('错误!');
                return false;
                }
        }
        </script>
<form id="form1" name="form1" method="post" action="" onsubmit='return check();'>
  <table width="270" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000033" style="padding:5px">
    <tr>
      <td align="center" bgcolor="#0066CC">报名序号:</td>
      <td bgcolor="#0066CC"><input size=20 name=T1 onkeyup="value=value.replace(/[^\d]/g,'')"   ></td>
    </tr>
    <tr>
      <td bgcolor="#0066CC">&nbsp;</td>
      <td bgcolor="#0066CC"><label>
        <input type="submit" name="button" id="submit" value="下一步"/>
      &nbsp;&nbsp;&nbsp;</label></td>
    </tr>  
  </table>
</form>
[/html]

FFEEDD 发表于 2008-8-28 14:38

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript">
function test(){
        var text=document.getElementById("text1").value;
        var num=/^1\d[1-9]$/;
        if(!text.match(num)){
                alert("输入错误,请输入大于100小于200的数字!");
        }
        else
                window.location.href='http://www.baidu.com';
}
</script>
<body>
<input type="text" id="text1" value=""><button onClick="test()">测试</button>
</body>
</html>

[/html]

wanghwei1 发表于 2008-8-28 14:43

谢谢高手指点,还有一点

谢谢高手指点,成功提交后能不能转向另个的页面

FFEEDD 发表于 2008-8-28 14:44

相关知识点请查阅相关资料:
1.string.match(rgExp)
2.window.location.href

FFEEDD 发表于 2008-8-28 14:44

回复 5# wanghwei1 [楼主] 的帖子

请看4楼

wanghwei1 发表于 2008-8-28 14:55

window.location.href这个函数

这个函数我在本地上测试能通过,但是传到服务器上有以下错误.
该页无法显示
您所查找的页面无法显示,因为使用了无效的方法(HTTP 动作)进行访问。
--------------------------------------------------------------------------------

请尝试以下操作:

如果您认为该请求应该被允许,请与网站管理员联系。
确保浏览器的地址栏中显示的网站地址的拼写和格式正确无误。
HTTP 错误 405 - 用于访问该页的 HTTP 动作未被许可。
Internet 信息服务 (IIS)

wanghwei1 发表于 2008-8-28 15:34

谢谢所有的兄弟,尤其是4楼大侠

小弟今天真是见牛人了,以后要好好学习

wanghwei1 发表于 2008-8-28 15:44

=/^1\d[1-9]$/ 不太明白

如果我想让数字大于0800001而小于0803000,是0开头的应该怎么写这个表达式,在哪里有这样函数的用法,请高手指点.

FFEEDD 发表于 2008-8-29 21:56

回复 10# wanghwei1 [楼主] 的帖子

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<script type="text/javascript">
function test(){
        var text=document.getElementById("text1").value;
        var num=/^0?80[0-2]\d{2}[2-9]$/;
        if(!text.match(num)){
                alert("输入错误,请输入大于0800001小于0803000的数字!");
        }
        else
                window.location.href='http://www.baidu.com';
}
</script>
<body>
<input type="text" id="text1" value=""><button onClick="test()">测试</button><br />请输入大于0800001小于0803000</body>
</html>
[/html]
我也是新手,不敢当。。。
这里用到的是正则表达式,你可以去百度一下就知道了。
=/^1\d[1-9]$/ 的意思是:第一个数字必须是1,第2个数字是0~9,第3个数字是1-9

[[i] 本帖最后由 FFEEDD 于 2008-8-29 22:00 编辑 [/i]]

页: [1]



Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.