www.webdeveloper.com
Results 1 to 10 of 10

Thread: Forms - Only letters or numbers!

  1. #1
    Join Date
    Jun 2008
    Posts
    270

    Forms - Only letters or numbers!

    I have this great piece of code that only allows letters in a form, however, it waits for you type the number and then gives it a null value (i think). If I wanted it to totally block any number keys (etc), how would I change it? AND, is there a way I could do this to only allow numbers???

    function validLetters(f)
    {
    !/^[A-zQ]*$/i.test(f.value)?f.value = f.value.replace(/[^A-zQ]/ig,''):null;
    }


    This code below totally blocks letters and signs! How would I apply that to the top code???????????

    function validNumbers(myfield, e, dec)
    {
    var key;
    var keychar;

    if (window.event)
    key = window.event.keyCode;
    else if (e)
    key = e.which;
    else
    return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
    return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
    return true;

    // decimal point jump
    else if (dec && (keychar == "."))
    {
    myfield.form.elements[dec].focus();
    return false;
    }
    else
    return false;
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    270
    This must be a hard one Anyone have a clue?

  3. #3
    Join Date
    Mar 2009
    Posts
    36
    My question to you is WHEN do you want to perform the check?

    I think preventing people to type anything you don't want ( like numbers ) can be done either onKeyPress or Onsubmit, where onsubmit check the values already entered ofc.

    /Fons

  4. #4
    Join Date
    Jul 2007
    Posts
    386
    First of all, you have to consider characters like Á are not typed with only one key. You use a combination depending on your keyboard configuration. Therefore using onkeyup is with the keyCode is not a good idea, since you will limit what the user can type to plain letters and numbers w/o special characters like É.

    This code is the best you can get:
    Code:
    function validLetters(f)
    {
        !/^[A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ]*$/i.test(f.value)?f.value = f.value.replace(/[^A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ]/ig,''):null;
    }
    As it simply removes unallowed characters after the user types it.

    Now, what I recommend is letting the user type anything he wants but warn him against any disallowed characters via an error message either on top, at the side or below the input element. Its just easier because it allows combination of keys to type in characters like Á.

    Use the keyCode method when you really only want basic letters, number or a combination of both and/or characters that can be type readily with one key stroke.

  5. #5
    Join Date
    Jun 2008
    Posts
    270
    How would I apply an alert(); message to this code? I am not too familiar with js, or I would simply do it myself.

    I tried:

    if (![A-zQ]) {
    alert(Enter only letters or die);
    }

    Didn't really work!

  6. #6
    Join Date
    Jun 2008
    Posts
    270
    Do you think the code I have for only allowing numbers is good or should I do some other approach?

  7. #7
    Join Date
    Aug 2007
    Posts
    3,767
    Strings need to be in quotes.
    Code:
    alert("Enter only letters or die");

  8. #8
    Join Date
    Jun 2008
    Posts
    270
    I just left the quotes out accident But I still wasn't able to get it to work...

  9. #9
    Join Date
    Aug 2007
    Posts
    3,767
    Oh, and you need a regular expression as well.
    Code:
    if (!/^[A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ]+$/gi.test(THE STRING)) {
    alert("Enter only letters or die");
    }
    [A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ] That means nothing, you need the //, and then ^ for the start, + for one or more, and $ for end.

  10. #10
    Join Date
    Jun 2008
    Posts
    270
    That explains it THX

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles