SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript replace

    Hey all,

    Right now I have a portion of a script that alerts when you aren't using numbers or letters into the text input. I want it so when it not only alerts, but removes the bad character. If I type in any of these.. -.)(*%^$#&^ it will say "You can only use numbers are letters". But I want to then automatically replace those illegal characters with nothing so the rest of the text stays in tact.

    Heres my code.

    Code JavaScript:
    var subdomain = document.getElementById('subdomain');  
    	var alphaExp = /^[0-9a-zA-Z]+$/;
     
    	if(!subdomain.value.match(alphaExp)){
    		alert('You can only use numbers and letters in your subdomain!');
    		return false;
    	} else {
    		var domain = document.getElementById('subdomain').value+'.'+document.getElementById('domain').value;
    		var url="checkdb.php?account="+domain;
    		xmlHttp.onreadystatechange=stateChanged;
    		xmlHttp.open("GET",url,true);
    		xmlHttp.send(null);
    	}

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should be

    subdomain.value = subdomain.value.replace(alphaExp, '');


  3. #3
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had tried that before. It doesnt seem to work Heres what I have now.

    Code JavaScript:
    	var subdomain = document.getElementById('subdomain');  
    	var alphaExp = /^[0-9a-zA-Z]+$/;
     
    	if(!subdomain.value.match(alphaExp)){
    		subdomain.value = subdomain.value.replace(alphaExp, '');
    		alert('You can only use numbers and letters in your subdomain!');
    		return false;
    	} else {
    		var domain = document.getElementById('subdomain').value+'.'+document.getElementById('domain').value;
    		var url="checkdb.php?account="+domain;
    		xmlHttp.onreadystatechange=stateChanged;
    		xmlHttp.open("GET",url,true);
    		xmlHttp.send(null);
    	}
    }

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    subdomain.value.replace(/\W/g, '');

    there, that's better.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great thanks! works good.

  6. #6
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replaced my longer code example with the correct replace regex. See if that works


Bookmarks

Posting Permissions

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