Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 27th, 2005, 01:23 PM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default checking for any whitespaces

 How can you check if an username or something like that contains no whitespaces? I have the code to replace al the whitespaces with any thing I want, but I prefer to notice the user they are using whitespaces and let them change it.


<input type="text" name="txtUsername" id="txtUsername" class="required" onblur="this.value = this.value.replace(/\s/gi, '_');" />



Thank you in advance!

Thijsje from the Netherlands!

 
Old September 7th, 2005, 05:58 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Furia

This function will check if the given username has any whitespace in it, or is empty. It uses the test function of the RegExp object, which returns true if the pattern exists in a given string.

function ContainsSpace(username)
{
    var re = /\s/i;
    return (username.length==0) || (re.test(username));
}

<input type="text" onblur="if(ContainsSpace(this.value))alert('invali d username')">

Hope this helps
Philip Cole




Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I keep leading and trailing whitespaces? 2BOrNot2B XSLT 2 October 31st, 2008 11:35 AM
Removing whitespaces with certain conditions trishla XSLT 16 October 4th, 2007 05:35 PM
preserving whitespaces in attribute values? kaps77 XSLT 7 August 13th, 2007 11:48 AM
Preserved leading and tailing whitespaces problem ivan603 XML 0 February 7th, 2006 10:18 PM
Where are my whitespaces going? dimondwoof XSLT 3 July 17th, 2003 08:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.