Solved

Using Regexp to search for a space

Posted on 2010-09-16
8
872 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:bupper
Comment Utility
or something like this:
regexp=[/?! /]

I didn't test them so you may need to experiment a little.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

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

Expert Comment

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

Author Comment

by:robertmayhue_iii
Comment Utility
Still has the opposite effect.
0
 
LVL 5

Accepted Solution

by:
bupper earned 125 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now