Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using Regexp to search for a space

Posted on 2010-09-16
8
Medium Priority
?
892 Views
Last Modified: 2012-05-10
I ahve a simple webpage that has a form. The form has a textbox on it and when it is submitted I need to check to see that the textbox has at least 1 letter, 1 number and no spaces in it. I have the following code to illustrate. I cannot get the space recognition on regexp to work. I have also attached the validation js file that I am using on the page.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="JavaScript" src="../js/validate.js" type="text/javascript"></script>
</head>
<body>
<form method="POST" name="form1">
      <p><input type="text" name="T1" size="20"><input type="submit" value="Submit" name="B1"></p>
</form>
<script language="JavaScript" type="text/javascript">
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("form1");
  frmvalidator.addValidation("T1","regexp=[A-Za-z]{1,15}","Enter a Letter");
  frmvalidator.addValidation("T1","regexp=[0-9]","Enter a Number");
  frmvalidator.addValidation("T1","regexp=[/\s/]","No spaces allowed");
</script>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="JavaScript" src="../js/validate.js" type="text/javascript"></script>
</head>
<body>
<form method="POST" name="form1">
	<p><input type="text" name="T1" size="20"><input type="submit" value="Submit" name="B1"></p>
</form>
<script language="JavaScript" type="text/javascript">
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("form1");
  frmvalidator.addValidation("T1","regexp=[A-Za-z]{1,15}","Enter a Letter");
  frmvalidator.addValidation("T1","regexp=[0-9]","Enter a Number");
  frmvalidator.addValidation("T1","regexp=[/\s/]","No spaces allowed");
</script>
</body>
</html>

Open in new window

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="JavaScript" src="../js/validate.js" type="text/javascript"></script>
</head>
<body>
<form method="POST" name="form1">
	<p><input type="text" name="T1" size="20"><input type="submit" value="Submit" name="B1"></p>
</form>
<script language="JavaScript" type="text/javascript">
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("form1");
  frmvalidator.addValidation("T1","regexp=[A-Za-z]{1,15}","Enter a Letter");
  frmvalidator.addValidation("T1","regexp=[0-9]","Enter a Number");
  frmvalidator.addValidation("T1","regexp=[/\s/]","No spaces allowed");
</script>
</body>
</html>

Open in new window

validate.js
0
Comment
Question by:robertmayhue_iii
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 5

Expert Comment

by:bupper
ID: 33693302
You could try this: Instead of "\s", try using " " (just a space) in your regex.

More helpful info here:
http://stackoverflow.com/questions/559363/matching-a-space-in-regex
0
 

Author Comment

by:robertmayhue_iii
ID: 33693684
When I use just a space it seems to act the opposite of what I want. I get the "No spaces allowed" alert when there is no space in the textbox and no alert when there is a space in the textbox.
0
 
LVL 5

Expert Comment

by:bupper
ID: 33693808
Ah, I see that as well. Just looking at the code, the regex is NEEDED to validate the form (for example just look at the one for the numbers).

Try a negative match, such as this:
regexp=[/^(?! )/]
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:bupper
ID: 33693829
or something like this:
regexp=[/?! /]

I didn't test them so you may need to experiment a little.
0
 

Author Comment

by:robertmayhue_iii
ID: 33693908
Thanks. I tried both of those and they don't work either.
0
 
LVL 5

Expert Comment

by:bupper
ID: 33694277
Ok here you go this should work:
regexp=[/^((?! ).)*$/]
0
 

Author Comment

by:robertmayhue_iii
ID: 33694527
Still has the opposite effect.
0
 
LVL 5

Accepted Solution

by:
bupper earned 500 total points
ID: 33694628
Here is a working PHP test:

echo 'match: ' . preg_match('/^((?! ).)*$/', 'this isatest');

You can experiment by removing the space in the string or leaving it in. Then port the regex over to your application! One of the things I notice is your regex has brackets around it, but for the php example above to work, you can't have brackets. So perhaps try:
regexp=/^((?! ).)*$/
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question