www.webdeveloper.com
Results 1 to 2 of 2

Thread: RegExp does not work

  1. #1
    Join Date
    Apr 2008
    Posts
    84

    RegExp does not work

    Code:
    <html>
    	<head>
    		<script>
    
    		function dt(){
    			var dt = new Date();
    			document.getElementById("time").innerHTML=dt;
    		}
    
    
    		function changeBgColor1(){
    				document.getElementById("bgc").style.backgroundColor='#ee0000';
    
    		}
    
    		function changeBgColor2(){
    				document.getElementById("bgc").style.backgroundColor='#880099';
    
    		}
    
    		function changeBgColor3(){
    				document.getElementById("bgc").style.backgroundColor='#888888';
    
    		}
    
    		function validate(){
    				var a = document.getElementById("input1").value;
    				var b = /[abcdef]/;
    				if(b.test(a)){
    					return true;
    				}
    				else {
    					alert("invalid character");
    				}
    		}
    		</script>
    
    		<style>
    			#bgc{
    				width:300px;
    				height:100px;
    				background-color:#cccccc;
    				font-weight:bold;
    				color:#ff00ff;
    			}
    			#time{
    				width:300px;
    				height:50px;
    				background-color:#aabb00;
    			}
    		</style>
    	</head>
    
    	<body onload="dt()">
    		<p id="time"> </p>
    		<p  id="bgc" onmouseover="changeBgColor1()" onmouseout="changeBgColor2()" onclick="changeBgColor3()">
    			Hi There, pls change bg color
    		</p>
    		<form id="form1" onsubmit="validate()">
    			<input id="input1" type=text />
    			<input id="input2" type="submit" value="Submit" />
    		</form>
    	</body>
    </html>
    validate function does not work with IE or Firefox, it always returns true, even if some digits are entered in input box (which should not be validated by regexp). Why does regexp not work ? How to make it work ?

    Thanks

  2. #2
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    450
    var b = /[abcdef]/;

    This checks whether the entered string contains anyone of the mentioned alphabets.

    If you want to check whether the entered string contains anything other than the mentioned alphabets you would use

    var b = /[^abcdef]/;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles