Javasrcipt Element is null error

trevor1940
trevor1940 used Ask the Experts™
on
Hi

I'm validating a form with javascript and getting a is null error
all i'm testing for is the word Invalid the Element Txt can either be empty or have text in it but not Invalid


if (document.getElementById('Txt').value.match(/[Invalid]/i)
   {
     alert(Txt + "Is Invalid")
      return false;
    }
when Txt is empty i get the error document.getElementById('Txt'). is null in FireFox error concole but the form submits.

how do i prevent this error?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
var value = document.getElementById('Txt').value;
if ( value == null || value == undefined || value.match(/[Invalid]/i)
   {
     alert(Txt + "Is Invalid")
      return false;
    }
Software Engineer
Commented:
document.getElementById('Txt') is null means DOM (Page) doesn't have the "Txt" element on it. If element is not in the DOM (Page), it's useless to show error message about it

Are you generating the page dynamically ?

-Kusala

Author

Commented:
Yes creating the page using ajax the user clicks a radio button this adds some additional options including the "Txt" Element

Author

Commented:
Need to re wright the code so i'm adding the txt element to the dom not justadding it with ajax

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial