/    Sign up×
Community /Pin to ProfileBookmark

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>[/CODE]

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@chrisranjanaApr 15.2012 — 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]/;
×

Success!

Help @jigneshjsoni spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...