如何只验证onkeypress的javascript中的字符 [英] how to validate only characters in javascript of onkeypress

查看:70
本文介绍了如何只验证onkeypress的javascript中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到如何验证代码请回复....

解决方案

请尝试以下代码



 <%@     Page    语言  =   C#    AutoEventWireup   =  true    CodeBehind   =  WebForm2.aspx.cs   继承  =  UserInterface.WebForm2   %>  

< !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 runat = server >
< title > < / title >
< script type = text / javascript >
功能 Valida te()
{
var charactersOnly = document .getElementById(' <%= txtName.ClientID%>')。value;
if (charactersOnly.search(/ ^ [a-zA-Z] +


/)=== -1)
{
alert( 仅字符);
document .getElementById(' <% = txtName.ClientID%>')。value = ;
}
}
< / script >
< / head >
< 正文 >
< < span class =code-leadattribute> form id = form1 runat = server >
< div >
< asp:TextBox ID = txtName runat = server onkeyup = 验证() > < / asp:TextBox >
< / div >
< ; / form >
< / body >
< / html >


浏览以下链接。



http://www.w3resource.com/javascript/form/all-letters-field.php [ ^ ]



希望这会有所帮助你

I am not getting how to validate the code please reply....

解决方案

Please try following code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="UserInterface.WebForm2" %>

<!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 runat="server">
    <title></title>
    <script type="text/javascript">
        function Validate()
        {
        var charactersOnly = document.getElementById('<%=txtName.ClientID %>').value;
        if (charactersOnly.search(/^[a-zA-Z]+


/) === -1) { alert("Only characters"); document.getElementById('<%=txtName.ClientID %>').value = ""; } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtName" runat="server" onkeyup="Validate()" ></asp:TextBox> </div> </form> </body> </html>


Go through the below link.

http://www.w3resource.com/javascript/form/all-letters-field.php[^]

Hope this will help you.


这篇关于如何只验证onkeypress的javascript中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆