locked
java script for field validation RRS feed

  • Question

  • Hii Everyone,

    this java script i hv written to check a field and to make a constraint that it should contain atleast one special character. bt its not working.

    please suggest to make a check to make it manadatory having a special character in it.

    function PolicyNumber() { 

     var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?"; 

    for (var i = 0; i<document.Policy.new_insurerpolicynumber.value.length; i++)

    {

    if (iChars.indexOf(document.Policy.new_insurerpolicynumber.value.charAt(i)) != -1)
    {
     }
    else
    {
    alert ("number should have atleast one special character");        
    return false;   
    }
                   }
    }

    please reply

    Thanks

     

    Tuesday, December 27, 2011 10:56 AM

Answers

  • hi

    can you try following

    function PolicyNumber() {
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
        var myFlag = false;
    
        var fieldObject = document.getElementById('new_insurerpolicynumber');
        if (fieldObject != null) {
            var strFieldValue = fieldObject.value;
            for (var i = 0; i < strFieldValue.length; i++) {
                if (iChars.indexOf(strFieldValue.charAt(i)) != -1) {
                    myFlag = true;
                    break;
                }
            }
        }
    
        if (myFlag == false) {
            alert("number should have atleast one special character");
            return false;
        }
        else {
            return true;
        }
    
    }
    

     

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

     


    Many Thanks -Bhautik Desai xRM Technologies

     



    Tuesday, December 27, 2011 11:20 AM

All replies

  • Hii change your code

     

    function PolicyNumber() { 
    
     var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?"; 
    var isFound=false;
    for (var i = 0; i<document.Policy.new_insurerpolicynumber.value.length; i++)
    
    {
    
    if (iChars.indexOf(document.Policy.new_insurerpolicynumber.value.charAt(i)) != -1)
    {
    isFound=true;
    break;
    }
     }
    if(isFound)
    {
    //do your code
    }
    else
    {
    alert ("number should have atleast one special character");        
    return false;   
    }
      
    }
    



    By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by san Sanz Tuesday, December 27, 2011 11:15 AM
    Tuesday, December 27, 2011 11:15 AM
  • hi

    can you try following

    function PolicyNumber() {
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
        var myFlag = false;
    
        var fieldObject = document.getElementById('new_insurerpolicynumber');
        if (fieldObject != null) {
            var strFieldValue = fieldObject.value;
            for (var i = 0; i < strFieldValue.length; i++) {
                if (iChars.indexOf(strFieldValue.charAt(i)) != -1) {
                    myFlag = true;
                    break;
                }
            }
        }
    
        if (myFlag == false) {
            alert("number should have atleast one special character");
            return false;
        }
        else {
            return true;
        }
    
    }
    

     

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

     


    Many Thanks -Bhautik Desai xRM Technologies

     



    Tuesday, December 27, 2011 11:20 AM
  • Thanks a lot Both of you...

    It worked correctly.

     

    Tuesday, December 27, 2011 12:40 PM