Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    how to use javascript to valida a text field value is a text

    hi

    i have a textfield i want the input be words instead of numbers and symbols

    how to do it?

    please help
    flying dagger

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Try this snippet

    Code:
    <script type="text/javascript">
    <!--
    function chkChar(obj){
    obj.value = obj.value.replace(/[^a-z\s]/gi,'')
    }
    // -->
    </script>
    
    <input type="text" value="" onkeyup="chkChar(this)">
    Last edited by Mr J; 01-02-2007 at 07:18 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    <HEAD>
    <SCRIPT type="text/JavaScript">
    function validate(f) {
    if (/[^A-z\s]/gi.test(f.value)) {
    alert("Only alpha characters and spaces are allowed!");
    f.value = f.value.replace(/[^A-z\s]/g,"");
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <input type="text" onkeyup="validate(this)">
    </BODY>

    There is a typo in Mr J's version:-

    function chkChar(obj){
    obj.value = obj.value.replace(/[^a-z\s]/gi,'')
    }
    Last edited by Philip M; 01-02-2007 at 07:13 PM.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    I think it was because I used the php tags it lost the backslash

    PHP Code:
    There should be a backslash between here  and here 
    See its gone and lost it again

    So I put my previous post in code tags
    Last edited by Mr J; 01-02-2007 at 07:24 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    if i also disallow space how
    flying dagger

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    remove \s in both versions:-

    <HEAD>
    <SCRIPT type="text/JavaScript">
    function validate(f) {
    if (/[^A-z]/gi.test(f.value)) {
    alert("Only alpha characters are allowed!");
    f.value = f.value.replace(/[^A-z]/g,"");
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <input type="text" onkeyup="validate(this)">
    </BODY>

    Or in Mr J's version:-

    function chkChar(obj){
    obj.value = obj.value.replace(/[^a-z]/gi,'')
    }


  •  

    Posting Permissions

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