www.webdeveloper.com
Results 1 to 2 of 2

Thread: Why warning message does not disappear?

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    5

    Why warning message does not disappear?

    In the script if user input contains space ' ' or does not contain @, then get warning.
    Problem is that if space is deleted, warning does not disappears. What need to change?

    HTML Code:
    <script type="text/javascript">
    <!-- 
    function checkEmail(email){
    if(email.length > 0) {
    if (email.indexOf(' ') >= 0)//The indexOf() method returns the position of the first occurrence of a specified value in a string.
    document.getElementById('myDiv1').innerHTML = 'email addresses cannot have spaces in them';
    else if (email.indexOf('@') == -1)
    document.getElementById('myDiv1').innerHTML = 'a valid email address must have an @ in it';
    else if (email.indexOf('@') == 1)
    document.getElementById('myDiv1').innerHTML = '';
    else if (email.indexOf(' ') == 0)
    document.getElementById('myDiv1').innerHTML = '';
    
    }
    }
    //-->
    </script>
    
    </head>
    
    <body>
    <form action="#" method=post>
    email:       <INPUT NAME="email" onChange="checkEmail(this.value)"><BR>
    </form>
    <div id="myDiv1"></div>

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    If you want to check for absence of a character, test that its index == -1, to check for presence of the character, test that its index != -1.
    Last edited by Logic Ali; 01-31-2013 at 02:31 PM.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

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