2 Replies - 1404 Views - Last Post: 13 June 2011 - 09:58 AM

#1 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

JQuery Regular Expression?

Posted 13 June 2011 - 09:24 AM

From what I could come up with in a Google search, I believe my code should work for what I'm trying to do, but alas.. it does not.

I have a form in which a user is to fill out a textbox. If he leaves it empty, an error message gets displayed. My definition of empty though is any number of spaces without anything else.

Here's my code:
$(document).ready(function(){
	$("#groupName").blur(function(){
		if($('#groupName').val().match(/^[[:space:]]*$/))
		{
			$("#ajaxError").html("Group Name field must be supplied");	
			$("#errorField").html("");
		}
		else 
		{
			$("#ajaxError").html("");	
			$("#errorField").html("");
		}
	});
 });



I should note, it worked fine when I wasn't trying to do a regular expression, so I know it is the regular expression that is the problem. This worked:
$(document).ready(function(){
	$("#groupName").blur(function(){
		if($('#groupName').val() == "")
		{
			$("#ajaxError").html("Group Name field must be supplied");	
			$("#errorField").html("");
		}
		else 
		{
			$("#ajaxError").html("");	
			$("#errorField").html("");
		}
	});
 });



What's going wrong? Thanks for the help!

This post has been edited by eZACKe: 13 June 2011 - 09:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: JQuery Regular Expression?

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: JQuery Regular Expression?

Posted 13 June 2011 - 09:36 AM

You can use trim.
Or, this regex:
([^\s]*)
alert(/([^\s])/.test("  y ")); 
alert(/([^\s])/.test("    "));

This post has been edited by japanir: 13 June 2011 - 09:37 AM

Was This Post Helpful? 1
  • +
  • -

#3 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: JQuery Regular Expression?

Posted 13 June 2011 - 09:58 AM

Thanks, I went with this and it's working great:
$(document).ready(function(){
	$("#groupName").blur(function(){
		if($('#groupName').val().trim() == "")
		{
			$("#ajaxError").html("Group Name field must be supplied");	
			$("#errorField").html("");
		}
		else 
		{
			$("#ajaxError").html("");	
			$("#errorField").html("");
		}
	});
 });


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1