- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 jquery 进行表单验证,我编写了一个函数来检查电子邮件是否已存在。当我在函数内部发出警报时,我得到一个 true 或 false 值,但是当我调用该函数时,我得到一个未定义的值。输入电子邮件地址后,我需要一个 true 或 false 值,有人可以帮助我吗?
我将代码添加到 fiddle 中:Here
HTML:
<div style="display: none;" id="dialog-form" title="Create new master admin">
<div class="validateTips">All form fields are required.</div>
<form id="target" method="post">
<fieldset>
<table width="100%">
<tr>
<td colspan="3">Please complete the form to create a new master admin (All form fields are required)</td>
</tr>
<tr>
<td height="20px"></td>
</tr>
<tr>
<td width="150px">Name</td>
<td colspan="2" valign="middle">
<input type="text" name="name" id="name" size="20" class="text ui-widget-content ui-corner-all" />
</td>
</tr>
<tr>
<td>Surname</td>
<td colspan="2">
<input type="text" name="password" id="password" size="20" class="text ui-widget-content ui-corner-all" />
</td>
</tr>
<tr>
<td>Email</td>
<td width="200px">
<input type="text" name="email" id="email" size="20" class="text ui-widget-content ui-corner-all" />
</td>
<td align="left"><span id="availability_status"></span>
</td>
</tr>
<tr>
<td>Cellphone</td>
<td colspan="2">
<input type="text" name="cellphone" id="cellphone" size="20" class="text ui-widget-content ui-corner-all" />
</td>
</tr>
<tr>
<td align="center" colspan="3">(A random username and password will be sent to the email provided)</td>
</tr>
</table>
</fieldset>
</form>
</div>
<button id="create-user">Create new user</button>
Jquery:
$(function () {
var name = $("#name"),
email = $("#email"),
surname = $("#password"),
cellphone = $("#cellphone"),
allFields = $([]).add(name).add(email).add(surname).add(cellphone),
tips = $(".validateTips");
function updateTips(t) {
tips.text(t)
.addClass("ui-state-highlight");
setTimeout(function () {
tips.removeClass("ui-state-highlight", 1500);
}, 500);
}
// Min max Length of field
function checkLength(o, n, min, max) {
if (o.val().length > max || o.val().length < min) {
o.addClass("ui-state-error");
updateTips("Length of " + n + " must be between " + min + " and " + max + ".");
return false;
} else {
return true;
}
}
// Check empty fields
function checkEmpty(o, n) {
if (o.val() == "") {
o.addClass("ui-state-error");
updateTips("please fill in " + n + ".");
return false;
} else {
return true;
}
}
// Check email
function checkEmail() {
//if ( o.val() == "") {
var email = $("#email").val();
var bValid = true;
$.ajax({ //Make the Ajax Request
type: "POST",
url: "master_admin_setup_submit.php", //file name
data: "email=" + email, //data
success: function (server_response) {
// alert(server_response);
if (server_response == '1') //if ajax_check_username.php return value "0"
{
$("#availability_status").html('<img src="../images/not_available.png" align="absmiddle"> <font color="red">Email already used</font>');
//add this image to the span with id "availability_status"
bValid = false;
return (bValid);
} else {
bValid = true;
//alert(server_response);
return (bValid);
}
//alert(checkEmail());
//o.addClass( "ui-state-error" );
//updateTips( "please fill in " + n + "." );
//return false;
//} else {
//return true;
//}
}
});
}
function checkRegexp(o, regexp, n) {
if (!(regexp.test(o.val()))) {
o.addClass("ui-state-error");
updateTips(n);
return false;
} else {
return true;
}
}
$("#dialog-form").dialog({
autoOpen: false,
width: 550,
modal: true,
resizable: false,
buttons: {
"Create master admin": function () {
var bValid = true;
allFields.removeClass("ui-state-error");
bValid = bValid && checkLength(name, "username", 3, 16);
bValid = bValid && checkLength(email, "email", 6, 80);
bValid = bValid && checkEmail();
alert(bValid);
bValid = bValid && checkLength(surname, "surname", 5, 16);
bValid = bValid && checkEmpty(cellphone, "cellphone");
// bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com");
//bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if (bValid == true) {
$.post('master_admin_setup_submit.php', $('#target').serialize(), function (result) {
// alert(result);
if (result === "1") {
$(function () {
$("#dialog-email").dialog({
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
});
} else {
$(function () {
$("#dialog-message").dialog({
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
});
// Reset form
$('#target')[0].reset();
// Close main dialog
$("#dialog-form").dialog("close");
}
});
}
},
Cancel: function () {
$(this).dialog("close");
}
}
});
$("#create-user")
.button()
.click(function () {
$("#dialog-form").dialog("open");
});
});
最佳答案
问题出在您的 checkEmail()
函数中。它本身不返回任何内容,因此如果在计算公式 bValid = bValid && checkEmail();
之前 bValid 为 true,则这将是未定义的。
如果您在函数内调用 $.ajax()
,默认情况下这是异步的,因此在验证您的数据的脚本完成工作后,您很可能会收到服务器返回的数据。换句话说,您的函数不会在 $.ajax()
上停止并等待结果,而是进一步运行,并且正在并行处理请求。您必须弄清楚如何让您的脚本等待服务器返回的数据。如果您只使用一个 ajax 验证器,它只需调用一个完成数据验证的函数,如果成功,则将数据发送到服务器。否则,您必须注意,结果可能会以随机顺序到达您的函数,因此也必须进行管理。
我希望它有帮助。
关于jquery - 获取未定义的值而不是 true 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21828387/
这个问题在这里已经有了答案: Why in Python does "0, 0 == (0, 0)" equal "(0, False)"? (7 个回答) 去年关闭。 代码片段 1: a = Tru
Integer i = 127; Integer j = 127; System.out.println(i == j); System.out.println(i.equals(j)); Integ
我试图用 Python 进行类似下面的代码的比较,但对产生的输出感到困惑。 谁能解释为什么输出是这样的? >>> True, True == True, True (True, True, True)
我们的下拉值是动态的 010100。 你能帮我将这些值转换为 true、false 吗? Offer的值是10100,Reject的值是10111。所以这些需要转换成 10100 = true,fal
我正在测试,如果用户在页面顶部显示一种货币“EUR”和另一种货币“GBP”,那么我期望包含文本“EUR”和页面下方还存在另一个包含文本“GBP”的链接。它包含在一个名为 "nav-tabs au-ta
如何检查数组的所有元素是真值还是假值。 因为以下内容似乎没有做到这一点:_.all([true, true, true], true); 它返回:false? 最佳答案 您应该重新阅读_.every(
C#:我有一个如下所示的字符串变量: string a = "(true and true) or (true or false)"; 这可以是任何东西,它可以变得更复杂,比如: string b
ruby : true == true == true syntax error, unexpected tEQ 对比JavaScript: true == true == true // => tr
这个问题已经有答案了: Equality of truthy and falsy values (JavaScript) (3 个回答) Which equals operator (== vs ==
为什么 R 中的 TRUE == "TRUE" 是 TRUE? R 中是否有与 === 等效的内容? 更新: 这些都返回FALSE: TRUE == "True" TRUE == "true" TRU
简单的查询,可能不可能,但我知道那里有一些聪明的人:) 给定一个 bool 参数,我希望定义我的 where 子句来限制特定列的输出 - 或不执行任何操作。 因此,给定参数@bit = 1,结果将是:
编写 Excel 公式时,将值设置为 true、“true”还是 true() 是否有区别? 换句话来说,以下哪一个是最好的?还是要看具体情况? if (A1 = 1, true, false) if
如果我们评估这个:TRUE AND TRUE,为什么会这样? 'yes' : 'no' 等于 TRUE 但不等于 yes 何时评估:(TRUE AND TRUE) ? 'yes' : 'no' 等于
这个问题在这里已经有了答案: Behaviour of and operator in javascript [duplicate] (1 个回答) 关闭 7 年前。 如题所说,我不太明白为什么(t
我有一个包含 FromDate 、 ToDate 、 VendorName 和 GoodsName 的表单,一旦一切为真,我需要显示结果 示例: FromDate="11/20/2019"、ToDat
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
我有一个模型,我有: ipv4_address = models.IPAddressField(verbose_name=_('ipv4 address'), blank=True, null=Tru
False in [True,True] False in pd.Series([True,True]) 第一行代码返回False第二行代码返回 True! 我想我一定是做错了什么或者遗漏了什么。当我
我可以在 Coq 中证明以下内容吗? Lemma bool_uip (H1 : true = true): H1 = eq_refl true. 即true = true 的所有证明都相同吗? 例如
如果我的理解是正确的,他们做的事情完全一样。为什么有人会使用“for”变体?仅仅是味道吗? 编辑:我想我也在考虑 for (;;)。 最佳答案 for (;;) 通常用于防止编译器警告: while(
我是一名优秀的程序员,十分优秀!