0

Dear Coders

I have following 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

Edited by tqmd1: n/a

2
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by hielo
0

try:

function checkpostal(){
  var re=/^[a-zA-Z]+$/; 
  var result=re.test(myform.myinput.value);
  if( result )
  { 
    alert("Good")
  }else{
    alert("Bad")
  }
return result;
}
0

Sir your codes work fine. Thanks for helping. I am new to Javascript Expression so please tell me about

/^[a-zA-Z]+$/;

what does it mean?

Please tell me about following three parts specially
/^
[a-zA-Z]
+$/

Thanks again

0

The whole expression makes sure that the input value
starts (^)
and ends ($)
with one or more (+)
upper or lower case letter ([a-zA-Z])

0

Dear Sir,
Will it validate digits form 0 to 9?

/^[0-9]+$/;
1

if you had:

var strA="abc123def";
var strB="123";

/* then the following would be used to check if str "contains" one or more consecutive digits */
var re=/[0-9]+/;

alert( re.test(strA) );//true - strA contains digits
alert( re.test(strB) );//true - strA contains digits

/* however, the following checks to see if it "consists entirely" (think "equals") of digits */
var re=/^[0-9]+$/;
alert( re.test(strA) );//false - strA does NOT consist entirely of digits
alert( re.test(strB)  );//true - strA consists entirely of digits


/* this one would be for a string that "ends" in digits (regardless of what is at the beginning) */
var re=/[0-9]+$/

/* this one would be for a string that "starts" with digits (regardless of what is at the end) */
var re=/^[0-9]+/

Look for online tutorials on regexs. It will be more productive for you if you first read up a "formal" regex tutorial.

0

Thanks sir for teaching me in a good way.
Now let me know about replace

var amount=9999999.999
how to replace it as
9,999,999.99

Thanks

0

Sir, I could not understand the meanin of following, pleaset tell me specially about word entirely.

var re=/^[0-9]+$/;alert( re.test(strA) );//false - strA does NOT consist entirely of digitsalert( re.test(strB)  );//true - strA consists entirely of digits
0

pleaset tell me specially about word entirely.

read the comment on line 10 again of that post again. It clearly states "think equals"

So if you had:

var str="abc123def";

Does that str "equals"/"consists entirely of" a string that is made up of digits only? No

//By contrast, this would be true:
var str="530";
var re=/^[0-9]+$/;
alert( re.test(strA) );

/* notice that str is made up of all digits.  It doesn't matter which digits or in which order.  All that the regex above cares about is that str consists only of digits. */
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.