emmaclarke Posted June 27, 2009 Author Share Posted June 27, 2009 Actually the alert message shows because inputting "MK16" == [0]=="M" && [1] == "K" && [2] == "1".So I need:[0] == M && [1] == K && [2] == 1,2,3,4,5,6,7,8,9 OR [2] == 1 && [3] == 0 (11,12,13,14,15,17,19) OR [2] ==7 && [3] == 7.I did try this but the OR || statements didn't work - the postcode should be MK1-15, MK17, MK19 or MK77. //conditions on beginning of the postcode to receive a special gift message, verifying each array element in the string for the position of the postcode if ((((((text[0] == "M" || text[0] == "m") && (text[1] == "K" || text[1] == "k") && (text[2] == "1" || text[2] == "2" || text[2] == "3" || text[2] == "4" || text[2] == "5" || text[2] == "6" || text[2] == "7" || text[2] == "8" || text[2] == "9") || (text[2] == "1" && text[3] == "0" || text[2] == "1" && text[3] == "1" || text[2]== "1" && text[3] == "2" || text[2] == "1" && text[3] == "3" || text[2] == "1" && text[3] == "4" || text[2] == "1" && text[3] == "5" || text[2] == "1" && text[3] == "7" || text[2] == "1" && text[3] == "9") || (text[2] == "7" && text[3] == "7")))))) { alert("you will receive a special gift"); } The OR statements between the third brackets onward doesn't seem to work. Link to comment Share on other sites More sharing options...
emmaclarke Posted June 27, 2009 Author Share Posted June 27, 2009 It still hits the same condition == [0] = M [1] = K [2] = 1 so:text[2] == "1" && text[3] == " " should by pass this so that MK1 is not the same as MK16 since the condition was just checking the first 3 chars. i.e. MK1 is true in the case of MK1 and MK16.I will see if this works. Link to comment Share on other sites More sharing options...
Ingolme Posted June 28, 2009 Share Posted June 28, 2009 These || and && are so accumulated that it's getting easy to make mistakes in any point.A regular expression should do the job // Do not split the text into an arrayvar reg = /^MK(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|17|19|77)\D/;/**** In english: MK followed by either of those numbers,*** making sure that there are no more digits than those required for the condition*/if(reg.test(text)) { alert("you will receive a special gift");} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.