Hi, Before answerng your actual question, a quick note on this:
> if (NameEntered.length>3 && [<% for code in @codes %>'<%= > code.name %>',<% end %>'test'].include(NameEntered)) { That looks to me as though it will follow all names, including the last one, with a comma, and so you'll end up with: if (NameEntered.length>3 && ['first','second','third',].include (NameEnetered)) { That will fail on IE (at least) because of the trailing comma; Firefox and some others will allow it. I'm also assuming that the names don't include any apostophes or backslashes, as you're not escaping them. Okay, to the actual question: In Prototype, Arrays mix in Enumerable, which has the Enumerable#any [1] method for looking through the enumration for any matching element. You can specify how the match is defined (probably via a regular expression[2]). E.g.,: if (myarray.any(function(item) { return /some expr/i.test(item); }) { // ...one of the elements matches the regex... } (Note I used the 'i' flag on the regex to ignore case.) That's pretty much it. What follows are just some ideas for making that clearer/cleaner. If that looks as ugly to you as it does to me (defining a function on- the-fly inside an if statement!), a couple of vars plus Function#bind [3] can help: var rematch = /some expr/i; var match = rematch.test.bind(rematch); if (myarray.any(match) { // ...one of the elements matches the regex... } Better yet, you could even make a factory for these: function makeRegexMatcher(re) { return re.test.bind(re); } ...then using it is easier: if (myarray.any(makeRegexMatcher(/some expr/i)) { // ... } (Last one) Or you could have a matching function like this: function matchesRegex(re, item) { return re.test(item); } ...and use it via Function#curry[4]: if (myarray.any(matchesRegex.curry(/some expr/i)) { // ... } [1] http://prototypejs.org/api/enumerable/any [2] https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp [3] http://prototypejs.org/api/function/bind [4] http://prototypejs.org/api/function/curry HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On May 10, 8:16 pm, Katherine <bridgeuto...@gmail.com> wrote: > Hi everyone. I wonder if there's less strict method for checking > whether a value exists or is included in an array. What I am looking > is similar to the SQL "LIKE" which is different from "=" > It would ignore spaces and not caps sensitive. > > I have this code > > ERB code : > > <%= text_field_tag('variation[][name]', variation.short_name, :size => > 30, :class => 'textInput', :id=>"varname_#{variation.id}") %> > > Prototype : > new Form.Element.Observer('varname_<%= variation.id %>', 0.3, function > (form, value){ > var NameEntered = $F('varname_<%= variation.id %>'); > > if (NameEntered.length>3 && [<% for code in @codes %>'<%= > code.name %>',<% end %>'test'].include(NameEntered)) { > $('error-messages-<%= variation.id %>').update('That product name > exists. ').style.color = 'red' > } > else { > $('error-messages-<%= variation.id %>').update('') > } > > }) > > I was checking whether the name is included in the array. Is it > possible to ignore spaces or capitalization so that the validation > would work better? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---