如何使用 livevalidation javascript 库 custom.validation 函数?
(注意:我原来的问题没有链接到我的 openid - 我在这里重新发布以便能够相应地编辑/更新/响应 - 如果任何有访问权限的人都可以删除原来的问题:/questions/1554916/how-to-use-the -livevalidation-javascript-library-custom-validate-function 那会很棒!!)
嘿伙计们,
我对这一切都很陌生,所以请耐心等待!
我设法创建一个表单,对字段进行实时验证检查,并使用 ajax/json 检查来查看用户名是否有效。我似乎对标准实时验证相处得很好。
这是我目前拥有的演示: 链接文本
对 ajax 用户名检查做出响应的方法只需更改 a 中的消息以使其可见,因此我想使用实时验证检查来管理 ajax 响应 - 这样我可以将它们链接在一起(检查空白,然后使用,然后无效,然后传递为ok)并让我的响应以相同的方式输出。
我目前有这个表单代码:
<?php
$script = "
$('uname').addEvent('blur', function(e) {
e = new Event(e).stop();
// Show the spinning indicator when pressing the submit button...
$('indicator1').setStyle('display', 'block');
var username = $('uname').value;
if ( username != '' ) {
var url = 'index.php?option=com_chronocontact&chronoformname=custom_livevalidation_test&task=extra&format=raw';
var jSonRequest = new Json.Remote(url, {
onComplete: function(r) {
$('indicator1').setStyle('display','none');
if ( r.username_ok ) {
$('username_msg').setHTML(\"<span style='color:green;'>Username '\"+username+\"' is OK</span>\");
} else {
$('username_msg').setHTML(\"<span style='color:red;'>Sorry, username '\"+username+\"' is already taken</span>\");
}
}
}).send({'username': username});
}
});
";
global $mainframe;
if ($mainframe->isSite())
{
$doc =& JFactory::getDocument();
$script = "
window.addEvent('domready', function() {
$script
});
";
$doc->addScriptDeclaration($script);
};
$script = "
var uname = new LiveValidation('uname', {
validMessage: 'This is a valid username',
wait: 500
});
uname.add(Validate.Presence, {
failureMessage: 'Username must not be blank!'
});
uname.add(Validate.Format, {
pattern: /^[a-zA-Z\-_]{4,16}$/,
failureMessage: 'Username must be between 4 and 16 characters'
});
";
global $mainframe;
if ($mainframe->isSite())
{
$doc =& JFactory::getDocument();
$script = "
window.addEvent('domready', function() {
$script
});
";
$doc->addScriptDeclaration($script);
};
?>
<div>
<span class="cf_label">Username: </span>
<input style="" id="uname" name="uname" class="" title="" value="" type="text">
<span id="indicator1" style="display: none"><img src="/images/hourglass.gif" alt="checking..." /></span>
<div id='username_msg'></div>
</div>
<br />
<input type='submit' name='submit' value='submit' />
这是在后台运行的 json 部分:
<?php
$json = stripslashes($_POST['json']);
$json = json_decode($json);
if ( $json->username ){
$db =& JFactory::getDBO();
$query = "
SELECT COUNT(*)
FROM `jos_users`
WHERE `username` = ".$db->quote($json->username).";";
$db->setQuery($query);
$response = (bool) !$database->loadResult();
$response = array('username_ok' => $response );
echo json_encode($response);
}
?>
所以查看 livevalidation 文档,您可以以这种方式使用自定义验证:
// Pass a function that checks if a number is divisible by one that you pass it in args object
// In this case, 5 is passed, so should return true and validation will pass
Validate.Custom( 55, { against: function(value,args){ return !(value % args.divisibleBy) }, args: {divisibleBy: 5} } );
我发现这非常神秘 - 我认为我应该能够指向“函数(r)”,这些都已经准备好了 - 但我怀疑我是否以正确的方式做这件事,
任何人都可以阐明,我(希望!)理解它并找到解决方案!
* 更新*
我现在相信 validate.custom 部分的代码应该可以工作:
var uname = new LiveValidation('uname', {
validMessage: 'This is a valid username',
wait: 500
});
uname.add(Validate.Presence, {
failureMessage: 'Username must not be blank!'
});
uname.add(Validate.Custom('uname', { against: function(r){
return !(r.username_ok) }, args: {} } ),
failureMessage: 'This username is already taken'
});
uname.add(Validate.Format, {
pattern: /^[a-zA-Z\-_]{4,16}$/,
failureMessage: 'Username must be between 4 and 16 characters'
});
";
但是,我似乎有一个架构问题 - 实时验证需要即时答案,而 ajax 发生在背景。我收到了一项研究“观察者模式”的建议,这对我来说是一个全新的概念 - 我通常会研究 cms 的图形设计和结构方面!
任何进一步的帮助/澄清表示赞赏,因为我将不得不回到这个并让它工作!
(note: my original question was not linked to my openid - I am reposting here to be able to edit/update/respond accordingly - if anyone with access could remove the original question: /questions/1554916/how-to-use-the-livevalidation-javascript-library-custom-validate-function that woudl be great !!)
Heya Folks,
I am very new to all of this so please bear with me!
I have mangaged to create a form with livevalidation checking of fields, and also with an ajax/json check to see if a usename is valid. I seem to get along fine with standard live validation.
Here is a demo of what I have at the moment: link text
The method to give a responce to the ajax username check simply changes a with a message in to make it visable, so I want to use a livevalidation check to manage the ajax responce - so I can link them together (check for blank, then in use, then invalid, then pass as ok) and have my responses output the same way.
I currently have this form code:
<?php
$script = "
$('uname').addEvent('blur', function(e) {
e = new Event(e).stop();
// Show the spinning indicator when pressing the submit button...
$('indicator1').setStyle('display', 'block');
var username = $('uname').value;
if ( username != '' ) {
var url = 'index.php?option=com_chronocontact&chronoformname=custom_livevalidation_test&task=extra&format=raw';
var jSonRequest = new Json.Remote(url, {
onComplete: function(r) {
$('indicator1').setStyle('display','none');
if ( r.username_ok ) {
$('username_msg').setHTML(\"<span style='color:green;'>Username '\"+username+\"' is OK</span>\");
} else {
$('username_msg').setHTML(\"<span style='color:red;'>Sorry, username '\"+username+\"' is already taken</span>\");
}
}
}).send({'username': username});
}
});
";
global $mainframe;
if ($mainframe->isSite())
{
$doc =& JFactory::getDocument();
$script = "
window.addEvent('domready', function() {
$script
});
";
$doc->addScriptDeclaration($script);
};
$script = "
var uname = new LiveValidation('uname', {
validMessage: 'This is a valid username',
wait: 500
});
uname.add(Validate.Presence, {
failureMessage: 'Username must not be blank!'
});
uname.add(Validate.Format, {
pattern: /^[a-zA-Z\-_]{4,16}$/,
failureMessage: 'Username must be between 4 and 16 characters'
});
";
global $mainframe;
if ($mainframe->isSite())
{
$doc =& JFactory::getDocument();
$script = "
window.addEvent('domready', function() {
$script
});
";
$doc->addScriptDeclaration($script);
};
?>
<div>
<span class="cf_label">Username: </span>
<input style="" id="uname" name="uname" class="" title="" value="" type="text">
<span id="indicator1" style="display: none"><img src="/images/hourglass.gif" alt="checking..." /></span>
<div id='username_msg'></div>
</div>
<br />
<input type='submit' name='submit' value='submit' />
With this being the json part that runs in the background:
<?php
$json = stripslashes($_POST['json']);
$json = json_decode($json);
if ( $json->username ){
$db =& JFactory::getDBO();
$query = "
SELECT COUNT(*)
FROM `jos_users`
WHERE `username` = ".$db->quote($json->username).";";
$db->setQuery($query);
$response = (bool) !$database->loadResult();
$response = array('username_ok' => $response );
echo json_encode($response);
}
?>
So looking at the livevalidation documentation, you can use a custom validation in this fashion:
// Pass a function that checks if a number is divisible by one that you pass it in args object
// In this case, 5 is passed, so should return true and validation will pass
Validate.Custom( 55, { against: function(value,args){ return !(value % args.divisibleBy) }, args: {divisibleBy: 5} } );
I am finding this to be very cryptic - I would think I should be able to point to 'function(r)' thats all ready there - but I doubt I am doing it the right way,
Can anyone shed any light, I am (hoping!) to understand it as well as uncover a solution!!
* Updates *
I am now led to believe that this code for the validate.custom part should work:
var uname = new LiveValidation('uname', {
validMessage: 'This is a valid username',
wait: 500
});
uname.add(Validate.Presence, {
failureMessage: 'Username must not be blank!'
});
uname.add(Validate.Custom('uname', { against: function(r){
return !(r.username_ok) }, args: {} } ),
failureMessage: 'This username is already taken'
});
uname.add(Validate.Format, {
pattern: /^[a-zA-Z\-_]{4,16}$/,
failureMessage: 'Username must be between 4 and 16 characters'
});
";
However it seems that I have an architecture issue - live validation wants an instant answer, and ajax takes place in the background. I have had a suggestion to investigate the 'observer pattern' thats an entirely new concept to me - I am usually playing with the graphical design and structure side of a cms!
Any further help / clarification appreciated as I will have to come back to this and get it working!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下链接中的代码示例。这对我有用。关键是将逻辑编写在单独的 JavaScript 函数中,并使用 Validate.Custom 函数调用它。
http://www.experts-exchange.com/codeSnippetPopup.jsp?csid=259114
编辑:从链接添加代码:
try the code sample in the following link. It worked for me. The key is to write the logic in a separate JavaScript function and call it with the Validate.Custom function.
http://www.experts-exchange.com/codeSnippetPopup.jsp?csid=259114
Edit: Added code from the link:
这是另一个有效的例子。
奇怪的部分是...
...我的 isValidCreditCard 函数嵌套在“反对”函数内。 against 函数仅返回我的 isValidCreditCard 函数返回的内容。
您可以将实际的函数放在那里,就像神秘的示例一样,但是超过一行的任何内容都可能会非常混乱。 (为什么在示例中使用 % [modulus]?没有人知道这个运算符!)
Here's another example that works.
The weird part is...
...which has my isValidCreditCard function nested inside the 'against' function. The against function just returns what my isValidCreditCard function returned.
You can put the actual function there instead just like the cryptic example, but anything more than a line would probably be quite confusing. (Why use % [modulus] in the example? No one knows that operator!)