0

i am trying to validate if a certain company was already picked for an application. the companyList format is:

60,261,420 ( a list of companyID)

I used

cID = $('#coName').val().split('::')[1]; 

to get the id only.

I am calling this function by passing say 60:

    findCompany = function(value) {
    var v = /^.+60,261,420$/.test(value);
    alert(v);   
}

when I pass the exact same string, i get false. any help?

5
  • What is "#companyList#" here? In other words, what does that regex look like when you do a view source on the page? Commented Dec 14, 2010 at 16:37
  • what is cfoutput tag for? Can you see what is the javascript in your browser, not in the source code. The tag will be substituted with something. Commented Dec 14, 2010 at 16:37
  • @Romario I'm guessing it's a ColdFusion thing. Commented Dec 14, 2010 at 16:38
  • sorry, yes it is in coldfusion Commented Dec 14, 2010 at 16:43
  • @Pointy, i edited my post to include what is in view source Commented Dec 14, 2010 at 16:44

2 Answers 2

1

Well if your company list is a list of numeric IDs like that, you need to make the resulting regular expression actually be the correct expression — if that's even the way you want to do it.

Another option is to just make an array, and then test for the value being in the array.

As a regex, though, what you could do is this:

var companyList = [<cfoutput> whatever </cfoutput>]; // get company ID list as an array of numbers
var companyRegex = new RegExp("^(?:" + companyList.join('|') + ")$");

Then you can say:

function findCompany(id) {
  if (companyRegex.test(id)) alert(id + " is already in the list!");
}
Sign up to request clarification or add additional context in comments.

2 Comments

sweet! companyRegex.text(id) should read companyRegex.test(id)
Also, note that as soon as you start getting a serious number of companies, this is going to get a little weird. It should work fine however up to a few hundred I guess. Make sure you try it with a big list of ids just so you know what happens!
0

Why not split the string into an array, like you did for your testing, iterate over the list and check if it's in?

A regexp just for that is balls, overhead and slower. A lot.

Anyway, for your specific question:

You’re checking the string "60" for /^.+60,261,420$/.

.+60 will obviously not match because you require at least one character before the 60. The commas also evaluate and are not in your String.

I don’t quite get where your regexp comes from.

Were you looking for a regexp to OR them a hard-coded list of IDs?

Code for splitting it and checking the array of IDs:

findCompany = function(value) {
  $('#coName').val().split('::').each(function(val){
    if(val == value) return true;
  });
  return false;
}

3 Comments

i am looking for a regexp. i tried to create one but it's not right obviously. the ids come from a query, then i convert to a list. i will give it a try for the array search
$ <- is that jQuery? Within your check-fn: $('#coName').val().split('::').each(function(val){ if(val == value) return true; }); return false;.
Ok, it’s not jQuery. What lib is it? It probably also has some each function? If not you’ll have to use JavaScripts for(var val in coNameText).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.