ASP texbox上的jQuery正则表达式验证. (使用验证器插件) [英] Jquery regex validation on asp texbox. (using validator plugin)
问题描述
我正在尝试在Visual Studio中验证多个asp文本框.我想使用jQuery,但我似乎无法实现它.我不断收到与
有关的错误
无法获取属性"form"的值:对象为null或未定义
有人可以给我明确的实施说明/代码以使其正常工作.我一直在尝试的示例将Validator.addmethod()用作尝试进行正则表达式验证的方法.
到目前为止,这是我的努力
Im trying to validate mutiple asp textboxes in visual studio. I want to use jquery but i really can''t seem to implement it. I keep getting errors relating to
Unable to get value of the property ''form'': object is null or undefined
Can someone please give me explicit implementation instructions / code to get it working. The examples ive been trying are usint the Validator.addmethod() as im trying to ad regex validation.
Here is my effort so far
var alNumRegex = /^[\d\d?]+$/; if (alNumRegex.test($(''txtStones'').val())) { alert("Invalid Input"); }
什么都没有
Does Nothing
$.validator.addMethod( "regex", function (value, element, regexp) { var check = false; var re = new RegExp(regexp); return this.optional(element) || re.test(value); }, "Please check your input."); $("#txtStones").rules("add", { regex: "^[\d\d?]+" })
给出错误-Microsoft JScript运行时错误:无法获取属性"form"的值:对象为null或未定义
并指向jquery.validate.js中的此代码
Gives error - Microsoft JScript runtime error: Unable to get value of the property ''form'': object is null or undefined
And points to this code in jquery.validate.js
rules: function(command, argument) {
var element = this[0];
if (command) {
var settings = $.data(element.form, 'validator').settings;
var staticRules = settings.rules;
var existingRules = $.validator.staticRules(element);
switch(command) {
case "add":
$.extend(existingRules, $.validator.normalizeRule(argument));
staticRules[element.name] = existingRules;
if (argument.messages)
settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages );
break;
特别是这行
This line in particular
var settings = $.data(element.form, 'validator').settings;
这是即时通讯使用的完整html页面.目前,我只是试图验证一个文本框以使其正常工作,然后我将再验证5个.
Here is the full html page im using. At the moment im just trying to validate one textbox to get it working then i will be validating a further 5.
<<pre lang="xml">%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="scripts/jquery-1.5.2.js" type="text/javascript"></script>
<script src="scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
$.validator.addMethod( "regex", function (value, element, regexp) { var check = false; var re = new RegExp(regexp); return this.optional(element) || re.test(value); }, "Please check your input."); $("#txtStones").rules("add", { regex: "^[\d\d?]+" })
//]]>
</script>
<script type="text/javascript">
</script>
<title></title>
<style type="text/css">
/*<![CDATA[*/
.style1 { width: 79%; } .style2 { width: 142px; }
/*]]>*/
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr><td class="style2"></td><td>.</td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td>Male Female</td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2">&nbsp;</td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
<tr><td class="style2"></td><td></td></tr>
</table>.
</div>
</form>
</body>
</html>
推荐答案
/;如果(alNumRegex.test(
/; if (alNumRegex.test(
(``txtStones'').val())){alert(无效输入"); }
(''txtStones'').val())) { alert("Invalid Input"); }
什么都没有
Does Nothing
.validator.addMethod("regex",函数(值,元素,正则表达式){var check = false; var re = new RegExp(regexp);返回this.optional (元素)|| re.test(值);},请检查您的输入.");
.validator.addMethod( "regex", function (value, element, regexp) { var check = false; var re = new RegExp(regexp); return this.optional(element) || re.test(value); }, "Please check your input.");
这篇关于ASP texbox上的jQuery正则表达式验证. (使用验证器插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!