Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2054338.ZTifN9JQ4c@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Tue, 01 Jan 2013 01:58:28 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <2012123116071843163-michaelfilbin@gmailcom> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 84 NNTP-Posting-Date: 01 Jan 2013 01:58:30 CET NNTP-Posting-Host: 9f621123.newsspool3.arcor-online.net X-Trace: DXC=CW`[Uh57m]PHigV@eW57PQMcF=Q^Z^V3X4Fo<]lROoRQ8kFQDZm8W4\YJN\b@mF9jNikdU3>YBYU3JEo]0]71R\Uij2Q X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17905 Please do not top-post, see , alternatively . Mike Filbin wrote: > On 2012-12-19 14:57:35 +0000, bravesplace@gmail.com said: >> I use the following to determine if a field is blank and to alert them >> the field is required: >> >> if (Form1.FieldName.value == "") >> { >> alert("Please enter a value for the FIELDNAME field."); >> Form1.FieldName.focus(); >> return (false); >> } >> >> How can I use a LIKE type of statement so if they use a word we cannot >> allow such as "Stupid", I can advise them the word is not permitted? > > Can you move your validation server-side? The validation code should be duplicated on, not moved to, the server side. In the best case there will be a server-side framework that generates the client-side code from server-side code. Because client-side validation can save unnecessary roundtrips to the server, improving user experience and network/server performance at the same time. Web applications 101. > If your form is rendered statically, You have earned one Bullshit Bingo point for “rendered statically”. What you perhaps meant to say was “_generated_ statically”. But that does not matter because: > the user can disable JavaScript to bypass your validation script. The user can disable client-side script support in any case, or their user agent might not be capable of executing the script code (for various reasons). That is why client-side validation helps, but does not suffice. > Also, just a quick tip. Cache your reference to 'Form1.FieldName'. It is > generally bad practice to resolve the same DOM node twice. You can cache > it like so: > > var field = Form1.FieldName; However, the proper, backwards-compatible and standards-compliant form is var field = document.forms["Form1"].elements["FieldName"]; whereas most of this is unnecessary if you pass the reference to the FORM element node with “this”: function checkForm (form) { var field = form.elements["FieldName"]; // … }
…
DOM 101. Finally, you failed to address the question. The simple and correct answer is, of course, regular expressions: if (/\bstupid\b/i.test(field.value)) { window.alert("The word 'stupid' is forbidden here."); return false; } However, if this is to prevent abuse, like spam, it is not going to be successful. Spammers rarely submit from the user perspective, they send HTTP requests. Which is why you can handle form abuse successfully only server-side. PointedEars -- When all you know is jQuery, every problem looks $(olvable).