Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Max Length 1

Status
Not open for further replies.

checkai

Programmer
Jan 17, 2003
1,629
US
I am looking to use an asp.net regular expression validation control on a text box. I would like to allow any character in the box, but a length of 0 to 200.

Ideas?

Thanks.

"...your mom goes to college..."
 
An asp.net regular expression validation, or a javascript regular expression validation?


If you just want the regex to ensure the length is 0 to 200 try something like this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function regex(obj) {
   var a = obj.value;
   if (!/^.{0,200}$/.test(a)) {
      alert("field must be between 0 and 200 characters");
      obj.focus();
      obj.select();
      return false;
   }
}

</script>

<style type="text/css"></style>

</head>
<body>

<input type="text" onchange="regex(this)" />

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
This seems to work. Thanks!

"...your mom goes to college..."
 
The only thing this doesn't allow is hard returns within a multi line text box. How can I allow these?

Thanks.

"...your mom goes to college..."
 
Well.... the period (.) matches any character other than a newline character. So, there's lots of ways to rewrite it to compensate. You could write the regex like this:
Code:
/(.|\n){0,200}/
or check for a decimal character or a non decimal character:
Code:
/(\d|\D){0,200}/
or an alphanumeric character and a non alphanumeric character:
Code:
/(\w|\W){0,200}/

They all pretty much do the same thing.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
when i attempt to use
Code:
/(.|\n){0,200}/
and put a hard return in the text box I get prompted that i'm not ok.

"...your mom goes to college..."
 
got it to work now...That last one actually does work, sorry! i had fat fingered it a little.

"...your mom goes to college..."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top