Solved

Using Regexp to search for a space

Posted on 2010-09-16
8
875 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

910 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

27 Experts available now in Live!

Get 1:1 Help Now