Results 1 to 3 of 3

Thread: Javascript indexof problems

  1. #1

    Thread Starter
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    Javascript indexof problems

    Hey, i'm having some problems with the javascript indexof function.

    I have this:
    Code:
    function checkinvalid() {
    var cde, def
    
    cde = document.getElementById('username');
    def = document.getElementById('password');
    
    if (cde.value.indexof == "<"||def.value.indexof == "<"||cde.value.indexof == ">"||def.value.indexof == ">") {
    	alert("The < or > character cannot be used");
    	return false;
    }else{
    	return true;
    }}
    Its returning true though :S I cant figure out why. I've tried escaping the < and > but its still not working. Anyone got any suggestions please??

    Thanks, BIOSTALL

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Javascript indexof problems

    Try this:

    Code:
    function checkinvalid() {
    var cde, def
    cde = document.getElementById('username');
    def = document.getElementById('password');
    if ((cde.value.indexOf("<") > 0) || (def.value.indexOf("<") > 0) || (cde.value.indexOf(">") > 0) || (def.value.indexOf(">") > 0)){
        window.alert("The < or > character cannot be used");
        return false;
    }
    else{
        return true;
    }}
    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    Re: Javascript indexof problems

    Cheers, code works a treat I just had to change the 0 to -1 so that it piked it up if > or < was the first or only character

    Thanks again, BIOSTALL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width