简体   繁体   English

仅Javascript登录字母

[英]Javascript Login alphabets only

This is my code and i can't connect my html to javascript I want to log in username and password with alphabets only 这是我的代码,我无法将html连接到javascript,我只想使用字母登录用户名和密码

<!DOCTYPE html>
<head>

<title>Login Form</title>
<script type="text/javascript" src="login.js">
</head>

<body>
<div id="dumdiv">
<form name="frm" method="post" action="login.php" id="f1" onkeyup="AllowAlphabet()">
    <table>
      <tr>
 <td class="f1_label">User Name :</td><td><input type="text" name="username" value="" />
            </td>
                </tr>
      <tr>

Password : 密码:

this is the javascript file

<script type="text/javascript">
function AllowAlphabet(){
           if (!frm.alphabet.value.match(/^[a-zA-Z]+$/) && frm.alphabet.value !="")
           {
                frm.alphabet.value="";
                frm.alphabet.focus(); 
                alert("Please Enter only alphabets in text");
           }
}      
</script>

您应该将onkeyup放在输入字段上而不是表格上

<input type="text" name="username" value="" onkeyup="AllowAlphabet()" />

Add function AllowAlphabet into your input element onkeyup event 将功能AllowAlphabet添加到输入元素的onkeyup事件中

<input type="text" name="username" value="" onkeyup="AllowAlphabet(this)" />

And change your function in javascript with this 并以此更改您在javascript的功能

function AllowAlphabet(ele){
   if (!$(ele).val().match(/^[a-zA-Z]+$/) && $(ele).val() !="")
   {
        $(ele).val("");
        alert("Please Enter only alphabets in text");
        $(ele).focus(); 
   }
}   

Fiddle: http://jsfiddle.net/T6U9G/1/ 小提琴: http : //jsfiddle.net/T6U9G/1/

UPDATE: 更新:

Finally your code will look like 最后,您的代码将如下所示

In your html: 在您的html中:

<form name="frm" method="post" action="login.php" id="f1">
    <table>
      <tr>
 <td class="f1_label">User Name :</td><td><input type="text" name="username" value="" onkeyup="AllowAlphabet(this)" />
            </td>
                </tr>
      <tr>
 <td class="f1_label">Password :</td><td><input type="password" name="password" value="" onkeyup="AllowAlphabet(this)" />
            </td>
                </tr>

And in your javascript: 在您的JavaScript中:

function AllowAlphabet(ele){
   if (!$(ele).val().match(/^[a-zA-Z]+$/) && $(ele).val() !="")
   {
        $(ele).val("");
        alert("Please Enter only alphabets in text");
        $(ele).focus(); 
   }
}   

If you use browser support HTML5 can use as 如果您使用浏览器支持,则HTML5可以用作

<input type="text" name="user_name" pattern="[A-Za-z]" title="Alphabet only">

else you can use as Peace answer 否则你可以用作和平答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM