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

try:

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

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

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

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

/^[0-9]+$/;

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.

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

Great

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

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. */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.