Results 1 to 2 of 2

Thread: javascript character validation

  1. #1
    Join Date
    Aug 2013
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default javascript character validation

    This script ensures that if the character length is not equal to 6. it will returns error message.
    Code:
    
    <script type="text/javascript">
    function validate(){
        x=document.myForm
        input=x.myInput.value
    
    
      //if (input.length != 6 OR 10){
    
    
    if (input.length != 6){
    
    
            alert("The field must contain 6 character or 10 character!")
            return false
        }else {
            return true
        }
    }
    </script>

    Now I want to return error message if character length is not equal to 6 or 10. how do i proceed

    Thanks

  2. #2
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Code:
    if (input.length >= 6 && input.length<=10){
            return true
        }else {
            alert("The field must contain 6 character or 10 character!")
            return false;
    }
    Meaning of code:
    if the input length is between 6 and 10 both inclusive then do this.
    else if it is less than 6 (not including 6) or greater than 10 (not including 10) do this.

    Or if you want something like this
    Code:
    if (input.length >= 6 && input.length<=10){
            return true
    }else if(input.length < 6){
            alert("The field is too short must contain at least 6 character!")
            return false;
    }else{
            alert("The field is too long must contain no more than 10 character!")
            return false;
    }
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2010, 08:26 AM
  2. Replies: 23
    Last Post: 02-04-2009, 07:52 AM
  3. javascript variable character limit??
    By LifeIsBeta in forum JavaScript
    Replies: 6
    Last Post: 04-25-2007, 12:18 PM
  4. Character map for special characters in javascript?
    By BonneSwe in forum JavaScript
    Replies: 1
    Last Post: 04-17-2007, 08:51 AM
  5. Exact character match validation
    By Jazz in forum JavaScript
    Replies: 10
    Last Post: 05-19-2006, 10:53 AM

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
  •