Jump to content

Find multiple texts/symbols in a string


stefan1294

Recommended Posts

Hello, I am pretty new to JavaScript, but I am working on a project. Currently I have to find a way to determ if a postalcode is an correct postal code. (A dutch postal code) For that I will have to confirm if two correct letters are entered in the form.I think my explanation is really unclear, so I will try to make it more clear for everyone. The postal codes in The Netherlands look like this: 1234 AB. In one place, e.g. Amsterdam, they use the letters "AA, AB, AC, AD, AG, AH, AJ" etc. The script has to look for one of those letters with the correct combination (AA, AB, AC, AD, etc). If I enter a different combination (e.g BA), it should give me the alert "This postal code is incorrect". I have absolutely no idea how to do this, and googling didn't do much as I do not know the keywords I have to look for. Here is an example of my current, not working, script:

var postalcodes = /(AA|AB|AC||AE||AH|AK|AL|AM|AN|AP|AR|AS|AT|AV|AW|AX|AZ|BA|BB|BC)/g;var a=document.forms["form"]["postcode"].value; if(a.match(postalcodes))  {   alert("Correct!");   return false; // Just doing return false to test the code  } 

I also tried "a.Search()", didn't work neither. Do you guys have any idea how I can do this?

Edited by stefan1294
Link to comment
Share on other sites

I think this might be part of the problem:AC||AE||AH You have a ! operator here, this means that the postal code is correct if the expression is not matched:!a.match(postalcodes)

  • Like 1
Link to comment
Share on other sites

No, the two bars are actually one of the mistakes in your expression. Have you read my post? One of the problems is that you're outputting "Correct" when the postal code is wrong.

  • Like 1
Link to comment
Share on other sites

No, the two bars are actually one of the mistakes in your expression. Have you read my post? One of the problems is that you're outputting "Correct" when the postal code is wrong.
I fixed that already :P - The real problem is not fixed :( Edited by stefan1294
Link to comment
Share on other sites

Be sure to check the error console.
I am not sure where I can find that :o
And it would help if you post your updated code here.
var postalcodes = /(AA||AB||AC||AE||AH||AK||AL||AM||AN||AP||AR||AS||AT||AV||AW||AX||AZ||BA||BB||BC)/g;var a=document.forms["form"]["postcode"].value;  if(a.search(postalcodes))  {alert("Correct!");return false;  }   --<tr><td>Postcode:</td><td><input type="text" name="postcode"></td></tr>

Nothing happens when I enter a postalcode (e.g. 7321AA or 7321 AA) Edit: At the moment it's saying "Correct" all the time, what ever I put in the text box/label

Edited by stefan1294
Link to comment
Share on other sites

If you're using Firefox, press Control+Shift+J to open the error console. Is this code in a function? You'll need to execute the code after the postal code has been written using an event handler.

  • Like 1
Link to comment
Share on other sites

If you're using Firefox, press Control+Shift+J to open the error console.
I am using Google Chrome. But for so far I can see, it's the same as in Firefox. And, it doesn't give me an error at all.
Is this code in a function? You'll need to execute the code after the postal code has been written using an event handler.
Yeah, the code is in a function.
 function validateForm()

For so far I know, it is being excecuted after the person pressed "Submit":

<form name="form" action="http://google.com" onsubmit="return validateForm()" method="post">

<input type="submit" value="Submit">

Edited by stefan1294
Link to comment
Share on other sites

OK, have you checked that a exists?
alert(a);

Yep, works fine Edit: I just did this to see what the result would be:
alert(a.search(postalcodes));

I entered "7856 AA" as postal code, and it alerted "0". I am not sure what to do with it though. It's supposed to say 6

Edited by stefan1294
Link to comment
Share on other sites

Try match() instead. Search could return a 0 even if the match is found in the string. You still have double vertical bars in your expression which I mentioned is a problem. You should only need single vertical bars. It's returning 0 because the first match for an empty string is at position zero. Your current expression is like this:var postalcodes = /(AA|[empty string]|AB|[empty string]|AC|[empty string] ...

  • Like 1
Link to comment
Share on other sites

If you haven't fixed your regular expression and simply switched to the match() method then it's going to return true even for invalid postal codes.

  • Like 1
Link to comment
Share on other sites

I am not sure what you are talking about, but it's actually working perfectly.

var postalcodes = /(AA|AB|AC|AE|AH|AK|AL|AM|AN|AP|AR|AS|AT|AV|AW|AX|AZ|BA|BB|BC|BD|BJ)/g;      if(!a.match(postalcodes))    { alert("Not correct!"); return false;    }

I entered: 7732 CA, it said "Not correct!"I entered 7732 BB and it didn't say anything

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...