...

View Full Version : Regular Expressions



tqmd1
12-29-2011, 04:46 AM
Dear Experts

I have folloiwng codes

<html>
<head>
<body>
<center>

<script language="JavaScript">
function checkpostal(){
var re=/^\D{A-Z}$/

if (myform.myinput.value.search(re)==-1)
{
alert("Good")
}else{
alert("Bad")
}
}
</script>

<form name="myform">
<input type="text" name="myinput" size=15>
<input type="button" onClick="checkpostal()" value="check">

</form>
</center>
</body>
</html>


With Onclick, I want to test input through regular expression
If entered values is between a-z or A-Z then
it must display
alert("Good")
else
alert("Bad")

Please help

Logic Ali
12-29-2011, 05:34 AM
You're using {} instead of [] for a range of characters. Also presumably you allow more than one letter so use the + quantifier.

You can check out RegExp documentation here (https://developer.mozilla.org/En/Core_JavaScript_1.5_Guide:Regular_Expressions).


<doctype html>
<head>
<body>

<script type="text/javascript">

function checkpostal( elem )
{
var re = /^[a-z]+$/i;

alert( elem.value.search( re ) == -1 ? "Bad" : "Good" );
}

</script>

<form name="myform" style='text-align:center'>
<input type="text" name="myinput" size=15>
<input type="button" onClick="checkpostal( this.form.myinput )" value="check">
</form>

</body>
</html>

Philip M
12-29-2011, 08:05 AM
You could improve the code by deleting all characters except letters and writing the result back to the field before making the test. Is there no limit to the number of letters which the user may enter?

Perhaps you are looking for

var re = /^[a-z]{2,5}$/i; // 2 to 5 letters case insensitive


You can test your regular expressions at: http://www.claughton.clara.net/regextester.html

hirecrishecom
12-29-2011, 09:54 AM
Thanx Actually i also face this kind of problem
After reading this my problem resolve......



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum