tags:

views:

31

answers:

3

I am trying to input validation of a form but when I debug my code the the debugger gives me an error of Incompatible Type. My code is below.

if(frm.input.value<'a' || frm.input.value>'z' || frm.input.value!='@' && frm.input.value!='.')
{

    alert("Not a valid E-mail adress");
}
A: 

That's not really going to work well cross browser. You should give your input element an id. Then you can do:

value = document.getElementById('myId').value;
if(value < 'a' || value > 'z' || value != '@' && value != '.'){
}

Also might want to check that condition. Are you missing brackets somewhere perhaps?

Evan Trimboli
+1  A: 

Those if's are checking the entire input string, not an individual character. Also I'm not sure if Javascript let's you do less than or greater than comparisons on characters, but I'll have to check that it may well let you.

If you are trying to validate an email address, your best solution would probably be using regular expressions.

If you want to do it this way, you are going to have to loop through each character of frm.input.value performing the checks which is slow and not a very good way of doing it.

Tom Gullen
You most definitely can, for example:var x = 'a';console.log(x < 'b');
Evan Trimboli
A: 

There are many problems with this code. To get started, I would suggest reading up on boolean logic. After that is well understood, I would suggest reading about strings and last but not least, the DOM.

Chris Shouts