Forum Moderators: open

Message Too Old, No Replies

JQuery Validation rules addMethod question

         

WoodNotOil

1:57 am on Feb 20, 2011 (gmt 0)

10+ Year Member



I have created a php filter for a form that returns an error message if someone uses profanity in the comments field and makes them redo that field before inserting into a database. Here is the code I use for that (I have omitted the lengthy list of profanity..):

$filter=array("badword1", "badword2");
for ($i = 0; $i < count($filter); $i++) {

if (preg_match("/\b".$filter[$i]."\b/i", $comments)) {

$profanity_msg="Profanity is not allowed";
}
}

//Then this before the form
if(isset($profanity_msg)){
echo $profanity_msg;
}

However, the rest of my form is validated through JQuery Validator and I would like to make a rule that would do the filtering instead of relying on the php. Here is a sketch of what I think I will need to do to build the custom rule, but I am not sure how to write the equivalent function to my php code above. Any help would be appreciated...

$(document).ready(function(){
//custom validation rule - profanity
$.validator.addMethod("profanity",

"Equivalent of my php function goes here..."

);

//validation implementation will go here.
$("#TestForm").validate({
rules: {
comments: {
required: true,
profanity: true
}
},
messages: {
comments: {
required: "* Required",
profanity: "* No Profanity Allowed"
}
}
});
})

JAB Creations

9:28 pm on Feb 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



*sigh*

Don't use jQuery, there are absolutely no good reasons to use it, it's buggy, it doesn't work cross-browser, too many people use it and there are too many conflicts and at the end of the day if I was to hire a programmer I'd hire the one who knows how to code correctly without burdening a project with 70 kilobytes (it's always 70KB) of doesn't-work.

Use my censor function to censor posts (badword1 becomes ********) or if you want to outright-deny a post and have the user manually correct their post just return false instead on the first match.

To use the function as-is simply give something with text (e.g. var censored_text = censor(document.getElementById('element_id').value)) and layoff the frameworks if you want to make progress without constantly backtracking.

- John

function censor(text)
{
var badwords = ['badword1','badword2','badword3'];

for (var i in badwords)
{
var word = badwords[i];
var replacement = word.substr(0,0);

for (var j = 1; j< word.length; j++) {replacement += '*';}

word = new RegExp(word,'gi');
text = text.replace(word,replacement);
}
return text;
}

TheMadScientist

12:22 pm on Feb 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it's always 70KB

Really?
[code.jquery.com...]

HTTP/1.0 200 OK
Accept-Ranges: bytes
Content-Encoding: gzip
Content-Type: application/x-javascript; charset=utf-8
Date: Sun, 27 Feb 2011 12:21:15 GMT
ETag: "32404f4-14d0c-a5998700+gzip"
Last-Modified: Wed, 23 Feb 2011 18:55:56 GMT
Server: ECS (ord/5734)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 29715
Connection: keep-alive

JAB Creations

12:17 pm on Feb 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes really, because I never see anything except one or multiple copies of 70KB sized jQuery being used.

It uses the unreliable non-standard innerHTML method.

It's 70KB of didn't do anything that requires even more code.

It adds another layer of dependencies.

It wastes bandwidth.

It deflects web designers and web developers from writing real code hollowing their ability to create real solutions.

There simply no reason to use frameworks.

- John