Solved

Using Regexp to search for a space

Posted on 2010-09-16
8
883 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
Industry Leaders: 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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bootstrap Form 16 42
Use javascript to open specific place on an accordian in other page 8 41
Clear input text 15 27
Sending ASP to server side 8 32
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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

726 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