Results 1 to 2 of 2

Thread: Javascript, If input cointains characters not declared on a list show message

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2013
    Location
    Spain
    Posts
    44

    Javascript, If input cointains characters not declared on a list show message

    Hi, I need a script that shows a message If an input (html) contains characters that are not on the list. At the moment I have this script:

    Code:
    var iChars = "ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
    for (var i = 0; i < document.formulario.usuario_nombre.value.length; i++) {
        if (iChars.indexOf(document.formulario.usuario_nombre.value.charAt(i)) != -1) {
            alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
    		document.formulario.usuario_nombre.focus()
            return false;
        }
    }
    That script "reads" the input and if a character matches with the written in the "iChars" variable, it shows a message, okay i need the opposite.. If any character in the input doesn't match with "iChars", show a message.. Thanks !

  2. #2

    Thread Starter
    Member
    Join Date
    Feb 2013
    Location
    Spain
    Posts
    44

    Re: Javascript, If input cointains characters not declared on a list show message

    Solved, just change the operator:

    Code:
    if (iChars.indexOf(document.formulario.usuario_nombre.value.charAt(i)) === -1) {

Posting Permissions

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



Click Here to Expand Forum to Full Width