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
Solved

Using Regexp to search for a space

Posted on 2010-09-16
8
880 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
  • 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
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.

 
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 125 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

860 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