佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 710|回复: 4

请教javascript 大大

[复制链接]
发表于 1-11-2008 12:15 PM | 显示全部楼层 |阅读模式
我本身是在学PHP 的 我上网找到这个script 来防止 别人打错字
但是有些防别人打错字的设定的key 我想改成我自己的 输入的文字 比例 第一个 name 只能 abc 和123
我想改成 多一个 - 但是我不会javascript.想请教大大 能解释下 这个script 的意思吗

<html>
<head>
<meta http-equiv="content-type" c>
<title>   </title>
<meta name="generator" c>
</head>
<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
<p><html>
<head>
<script type="text/javascript"><!--
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

//-->
</script>
<script type="text/javascript"><!--
function restrictmail(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// alphas and numbers
else if ((("
[email=abcdefghijklmnopqrstuvwxyz0123456789_-@.]abcdefghijklmnopqrstuvwxyz0123456789_-@.").indexOf(keychar[/email]) > -1))
   return true;
else
   return false;
}
//-->
</script>
<script type="text/javascript"><!--
function restrictuser(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// alphas and numbers
else if ((("abcdefghijklmnopqrstuvwxyz0123456789_").indexOf(keychar) > -1))
   return true;
else
   return false;
}
//-->
</script>
<script type="text/javascript">
<!--
function restrictsqa(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// alphas and numbers
else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) > -1))
   return true;
else
   return false;
}
//-->
</script>

</head>
</p>
<body>
<table width='431' height='318' border='0' cellpadding='0' cellspacing='0'>
    <tr>
        <td height='27' align='center' valign='bottom'><img src="images/h2_register.gif" width='431' height='27' /></td>
    </tr>
    <tr>
        <td height='6' align='center' valign='bottom'><img src="images/bg_notice_top.gif" width='431' height='6' /></td>
    </tr>
    <tr>
        <td height='275' align='center' valign='top' background="images/bg_notice_main.gif">
            <form id='reg_form' name='reg_form' method='post' action='index.php?op=register_go'>
                <table width='431' border='0' cellspacing='0' cellpadding='1'>
                    <tr>
                        <td width='138' height='26' align='right'> </td>
                        <td width='8'> </td>
                        <td width='128' align='left'> </td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'> </td>
                    </tr>
                    <tr>
                        <td width='138' align='right'>Username :</td>
                        <td width='8'> </td>
                        <td width='128' align='left'><input name='username' type='text' id='username' size='20' maxlength='10' onkeypress='return restrictuser(event)' /></td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'>4 to 10 characters!</td>
                    </tr>
                    <tr>
                        <td width='138' align='right'>Password :</td>
                        <td width='8'> </td>
                        <td width='128' align='left'><input name='password' type='password' id='password' size='20' maxlength='16' onkeypress='return restrictuser(event)' /></td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'>4 to 10 characters!</td>
                    </tr>
                    <tr>
                        <td width='138' align='right'>Repeat Password :</td>
                        <td width='8'> </td>
                        <td width='128' align='left'><input name='repassword' type='password' id='repassword' size='20' maxlength='16' /></td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'> </td>
                    </tr>
                    <tr>
                        <td width='138' align='right'>Active E-mail :</td>
                        <td width='8'> </td>
                        <td width='128' align='left'><input name='email' type='text' id='email' size='20' maxlength='40' onkeypress='return restrictmail(event)' /></td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'> </td>
                    </tr>
                    <tr>
                        <td width='138' align='right'>Secret Code :</td>
                        <td width='8'> </td>
                        <td width='128' align='left'><input name='scode' type='password' id='scode' size='20' maxlength='10' onkeypress='return restrictuser(event)' /></td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'>4 to 10 characters!</td>
                    </tr>
                    <tr>
                        <td width='138' align='right'>Repeat Code :</td>
                        <td width='8'> </td>
                        <td width='128' align='left'><input name='rescode' type='password' id='rescode' size='20' maxlength='10' /></td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'> </td>
                    </tr>
                    <tr>
                        <td width='138' align='right'> </td>
                        <td width='8'> </td>
                        <td width='128' align='left'> </td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'> </td>
                    </tr>
                    <tr>
                        <td width='138' align='right'> </td>
                        <td width='8'> </td>
                        <td width='128' align='center'><input type='submit' name='submit' id='button' value='submit' />
                      <input type='reset' name='clear' id='clear' value='clear' /></td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'> </td>
                    </tr>
                    <tr>
                        <td width='138' align='right'> </td>
                        <td width='8'> </td>
                        <td width='128' align='left'> </td>
                        <td width='8' align='left'> </td>
                        <td width='139' align='left'> </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
    <tr>
        <td height='9' align='center' valign='top'><img src="images/bg_notice_bottom.gif" width='431' height='9' /></td>
    </tr>
</table>
</body>
<p></html></p>
<p> </p>
</body>
</html>
回复

使用道具 举报


ADVERTISEMENT

发表于 1-11-2008 12:29 PM | 显示全部楼层
用regular expression會比較好....
http://www.javascriptkit.com/javatutors/re.shtml
回复

使用道具 举报

发表于 3-11-2008 11:26 PM | 显示全部楼层
为什么不用 PHP ctype_digit, ctype_alpha, ctype_alnum?

[ 本帖最后由 wakasu 于 3-11-2008 11:32 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 6-11-2008 09:36 AM | 显示全部楼层
    PHP 的ctype_digit, ctype_alpha, ctype_alnum

我还没学过 我收下了 谢谢wakasu   我还是php新手。。学了几个月的php
回复

使用道具 举报

发表于 8-11-2008 09:53 AM | 显示全部楼层
Javascript 怎么说都是Client Side 的。既然你学了PHP,就用PHP来validate吧。用Regular Expression是比较快的方法。但是需要时间学习它的Syntax。

给你一个Example吧,用来validate名字。

大致上是说所需的yourName是0-9, a-z, A-Z 和 \s(空格)

if(!preg_match("/^[0-9a-zA-Z\s]$/", $_POST['yourName'])){
     echo "名字不正确!";
}
else{
    echo "名字OK!";
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 07:29 AM , Processed in 0.128999 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表