Regular expression search not working

D

donpro

Hi,

I have a TEXTAREA where I wish to ensure that it contains at least on
4 digit number. The following code snippet always returns -1 even when
the 4 digit number is present and I don;t know why. Note that I first
ensure that the vale is not empty but I am not showing that code in
the snippet below.

var hsCodeElement = document.getElementById('TaField));
var hsCodePattern = '/^\d{4}$/';

if (hsCodeElement.value.search(hsCodePattern) == -1) {
alert(hsCodeElement.value.search(hsCodePattern) + ": HS Codes must
contain at least entered one 4 digit numeric value");
setTimeout(function() {hsCodeElement.focus();}, 10);
}
 
M

Martin Honnen

donpro said:
I have a TEXTAREA where I wish to ensure that it contains at least on
4 digit number. The following code snippet always returns -1 even when
the 4 digit number is present and I don;t know why. Note that I first
ensure that the vale is not empty but I am not showing that code in
the snippet below.

var hsCodeElement = document.getElementById('TaField));
var hsCodePattern = '/^\d{4}$/';

Drop the single quotes, a regular expression literal in Javascript s
delimited by slashes i.e.
var hsCodePattern = /^\d{4}$/;
I am not sure however you want the achors ^ and $ as you say "contains
at least on[sic] 4 digit number" while the expression you have written
basically says "is exactly a 4 digit sequence" and contains nothing
before or after that.

if (hsCodeElement.value.search(hsCodePattern) == -1) {

if (hsCodePattern.test(hsCodeElement.value)) {

seems easier.
 
D

Denis McMahon

I have a TEXTAREA where I wish to ensure that it contains at least on 4
digit number.
var hsCodePattern = '/^\d{4}$/';

If you mean "at least one sequence of 4 digits"

var hsCodePattern = /\d{4}/;

If you mean "at least one sequence of exactly 4 digits"

var hsCodePattern = /(^|\s)\d{4}(\s|$)/;

a) Don't quote the regex, it's a regex, not a string containing a regex.
b) If you include start and end anchors, your pattern is treated more
like "consists of exactly 4 digits" rather than "contains a 4 digit
sequence".

This html will allow you to evaluate the contents of a text area against
4 different "find 4 digit" regexes.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Test RegExes</title>
<script type="text/javascript">
var regExes = new Array (/^\d{4}$/, /(^|\s)\d{4}(\s|$)/,
/\s\d{4}\s/, /\d{4}/);
function dgebi(id)
{
return document.getElementById(id);
}
function testRegExes()
{
var ta = dgebi('theTextArea');
var re, alert;
for (re = 0; re < regExes.length; re++)
{
alert = dgebi("alert_" + re);
if (ta.value.match(regExes[re]))
{
alert.style.backgroundColor = "#00ff00";
}
else
{
alert.style.backgroundColor = "#ff0000";
}
}
}
</script>
<style type="text/css">
p {margin: 20px auto 0 auto; text-align: center;}
table {margin: 20px auto 0 auto; border-collapse: collapse;}
th, td {text-align: center;border: thin solid black;}
</style>
</head>
<body onload="testRegExes()">
<p>
<textarea cols="60" rows="6" id="theTextArea"></textarea><br>
<input type="button" value="Evaluate" onclick="testRegExes()">
</p>
<table width="80%">
<tr>
<td width="25%" id="alert_0">/^\d{4}$/</td>
<td width="25%" id="alert_1">/(^|\s)\d{4}(\s|$)/</td>
<td width="25%" id="alert_2">/\s\d{4}\s/</td>
<td width="25%" id="alert_3">/\d{4}/</td>
</tr>
</table>
<table width="30%">
<tr>
<th width="100%">Color Key</th>
</tr>
<tr>
<td style="background-color: #00ff00">RegEx Matched</td>
</tr>
<tr>
<td style="background-color: #ff0000">RegEx Not Matched</td>
</tr>
</table>
</body>
</html>

Rgds

Denis McMahon
 
J

Jukka K. Korpela

If you mean "at least one sequence of exactly 4 digits"

var hsCodePattern = /(^|\s)\d{4}(\s|$)/;

That's really "at least one sequence of exactly 4 digits surrounded by
whitespace or start or end of string"; for example, "a1234" does not
match, and neither does "1234-5" or "1234.".

If you want "at least one sequence of 4 digits that is not part of a
longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.
 
L

Lasse Reichstein Nielsen

Jukka K. Korpela said:
If you want "at least one sequence of 4 digits that is not part of a
longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.

Or /\b\d{4}\b/

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top