JavaScript Regular Expression problem

Results 1 to 2 of 2

Thread: JavaScript Regular Expression problem

  1. #1
    Join Date
    Dec 1969

    Default JavaScript Regular Expression problem

    Hi all,<BR>probably a very easy problem for someone who knows the answer! except that I don&#039;t at the moment....<BR>Below is a section of JavaScript code. The value in txtCallSign should be of the format M0xxx where the layout is the first letter should be an &#039;m&#039;, second char should be a 0,1 or 5, followed by 3 chars. Not worried about case sensitivity. I have got the RegExp as close as I can get it, but I don&#039;t know how to limit the number of chars they can type in. ie: my code allows MM1WER, or M11WEQ, etc.<BR><BR>var strCallSign = document.frmAMRenewal.txtCallSign.value;<BR>var expCallSign = new RegExp(/[m]+[015]+[a-z]/i)<BR>var lResult=expCallSign.test(strCallSign);<BR> if (lResult==false) {<BR> alert("The format is wrong - please correct");<BR> return false;<BR> }<BR><BR><BR>Thanks for any help,<BR>Andy

  2. #2
    Join Date
    Dec 1969

    Default RE: JavaScript Regular Expression problem

    This is happening because you&#039;re using the + quantifier, which means "match the preceeding expression one or more times". The default (ie no quantifier) is to match exactly once. You need to use a quantifier of the form {n} to match an expression exactly n times. Try this one:<BR><BR>/m[015][a-z]{3}/i<BR><BR>Dunc

Posting Permissions

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