Results 1 to 7 of 7

Thread: validating in JS- no spaces in a word

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default validating in JS- no spaces in a word

    I want to check some form input, that it should be only one word without spaces and that it will be at least 6 characters.

    I succeeded checking that length of the word, but didn't know what to do about the spaces.

    my function:

    PHP Code:
    // If it is at least 6 characters it attaches class="welldone" to the containing fieldset.

    function checkUsernameForLength(whatYouTyped) {
        var 
    fieldset whatYouTyped.parentNode;
        var 
    txt whatYouTyped.value;
        if (
    txt.length 5) {
            
    fieldset.className "welldone";
        }
        else {
            
    fieldset.className "";
        }


  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Just replace the spaces using this method: http://www.tizag.com/javascriptT/jav...ng-replace.php
    Jeremy | jfein.net

  3. #3
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Just replace the spaces using this method: http://www.tizag.com/javascriptT/jav...ng-replace.php
    hi thanks but i don't want to replace it, i want to write some condition which will check it

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Use indexOf().
    Code:
    if(!(txt.indexOf('')==-1))
    	{
    	alert('spaces is present');
    	}
    Hope it helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. The Following User Says Thank You to rangana For This Useful Post:

    d-machine (06-14-2008)

  6. #5
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there d-machine,

    this will check for 6 or more letters...
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    window.onload=function() {
    document.forms[0][1].onclick=function() {
    if(document.forms[0][0].value.match(/^[a-zA-Z]{6,}$/)){
        alert('the input matches the requirement');
     }
    else {
       alert('the input does not match the requirement');
       }
      }
     }
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div>
     <input type="text">
     <input type="button" value="check">
    </div>
    </form>
    
    </body>
    </html>
    
    coothead

  7. The Following User Says Thank You to coothead For This Useful Post:

    d-machine (06-14-2008)

  8. #6
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Thank you both !!
    You are awesome I really appreciate your help.

  9. #7
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    No problem, you're welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •