0

I am reading a text field, and converting that value to Lower case and comparing to the array of words, if it matches it throws error.

But the problem is when we enter a text as a Alpha Numeric value, then I am not able to convert Lower case letters and getting the Run Time error as "Object is Not Defined"

if(streetAddress != null){
    for(var k=0; k<notValidPostalChars.length; k++){

        var secondWord = notValidPostalChars[k];

        if (streetAddress.toLowerCase().startsWith(stopWord.toLowerCase())) {
            alert("Invalid Error Message");
            document.getElementById("address").focus();
        return false;
        }

    }
}

In the above Example, streetAddress may contain the Alpha Numeric as well and it might be in the Lower case or Upper case letters.

This will be entered by the End users and notValidPostalChars is an Array consisting of all the Pre defined Words with UPPER CASE letters

gmhk
  • 15,598
  • 27
  • 89
  • 112
  • how do you assign a value to `streetAddress` ? what is in `stopWord` (*maybe it should be `secondWord`*) ? – Gabriele Petrioli May 30 '11 at 08:57
  • If you are getting the value of a form control, it is always a string even if it contains digits. Show some sample input and errors. – RobG May 30 '11 at 08:58
  • alert(streetAddress.toLowerCase()); for debugging ans same for stopword – Ibu May 30 '11 at 08:59
  • Alpha numeric strings are able to convert to lowercase: http://jsfiddle.net/jMBqX/. So, perhaps there's another problem? – peterp May 30 '11 at 08:59
  • Please try to supply clear mesSages to your users: 'Not invalid mesage' means it's a valid message. – KooiInc May 30 '11 at 09:00
  • if your stopWord is some example div id then simply add some dummy character like div id='aWORD' – Max May 30 '11 at 09:04
  • @peterp, I think you are correct – gmhk May 30 '11 at 09:13
  • @harigm I assume you have a function startsWith defined? [startsWith](http://stackoverflow.com/questions/646628/javascript-startswith) is not core javascript. Instead it is indexOf()===0 – mplungjan May 30 '11 at 09:30

1 Answers1

0

Maybe stopWord is not defined?

var secondWord = notValidPostalChars[k];
if (streetAddress.toLowerCase().startsWith(stopWord.toLowerCase())) {
//                                         ^did you mean secondWord?
   alert("Not invalid mesage");
//       ^ did you mean 'invalid message / not a valid message'?
   document.getElementById("address").focus();
   return false;
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • SopWord is your secondWord, I am reading the data from the array and comparing one by one – gmhk May 30 '11 at 09:11
  • Why then, are you assigning `secondWord` in the loop? – KooiInc May 30 '11 at 09:14
  • Read the Street address from the jsp page, Not valid Postal code is an array which has all Not valid Characters, i am reading one item from array and checking whether if street address is starting with it or not?? – gmhk May 30 '11 at 09:17
  • No, that's not what you do: you compare the first character of `streetAddress` to `stopWord` - whatever that may be. Try `streetAddress.toLowerCase().startsWith(secondWord.toLowerCase())` – KooiInc May 30 '11 at 09:22
  • Let me try this now, In the mean time, one of the second word array item has "/" character, do you suspect this might be the reason for the "Object is Not Defined" – gmhk May 30 '11 at 09:26
  • I don't think so (`'/'.toLowerCase()` is no problem). I think you should check the `startsWith` function. Test without it, using: `streetAddress.substr(0,1).toLowerCase() === secondWord.toLowerCase()` – KooiInc May 30 '11 at 09:34