locked
character delete problem javascript RRS feed

  • Question

  • hey i have a function which replace "-" to ""

    but when i save and refresh the form it continous to save "-" character.i want to delete "-" character if it entries by user.

    here is my code below whats wrong?

      function Validatenumber()

     {
       var value1=crmForm.all.new_customernumber;
    var result=value1.value.length;
    if(result!=10)
    {
    alert("Invalid customer number!");
    for(i=0;i<result;i++)
    {
    value1.value=value1.value.replace("-", "");

    }

     Xrm.Page.getAttribute("new_customernumber").setValue(value1);
    }
    }


    Crm Software Specialist




    Wednesday, September 26, 2012 6:59 AM

Answers

  • Here is a modified version of the script function.

    function Validatenumber() {
        var value1 = crmForm.all.new_customernumber;
        var result = value1.value.length;
        if (result != 10) {
            alert("Invalid customer number!");
            for (i = 0; i < result; i++) {
                value1.value = value1.value.replace("-", "");
    
            }
    
            Xrm.Page.getAttribute("new_customernumber").setValue(value1.value);
        }
    }

    I have also noticed replace does not automatically replace all occurrences. I thought it did. But it explains the iteration.

    You can also rewrite the iteration using a pattern match and replace like so.

    function Validatenumber() {
        var value1 = crmForm.all.new_customernumber;
        var result = value1.value.length;
        if (result != 10) {
            alert("Invalid customer number!");
                value1.value = value1.value.replace(/-/ig,"");
    
    
            Xrm.Page.getAttribute("new_customernumber").setValue(value1.value);
        }
    }

    • Edited by Oscar Wahlen Wednesday, September 26, 2012 9:48 AM
    • Proposed as answer by Oscar Wahlen Wednesday, September 26, 2012 12:33 PM
    • Marked as answer by Ahmet Çankaya Wednesday, September 26, 2012 1:43 PM
    Wednesday, September 26, 2012 9:40 AM

All replies

  • Hi,

    I have a few remarks concerning your script.

    1) Why is the character replacement inside an iteration? It does not make sense to me.

    2) Why are you not storing the replacement result in a separate variable. It makes you script more readable.

    3) Why are you passing the control instance to the setValue function instead of the value attribute (e.g. value1.value)?

    Wednesday, September 26, 2012 7:35 AM
  • Hi,

    I have a few remarks concerning your script.

    1) Why is the character replacement inside an iteration? It does not make sense to me.

    2) Why are you not storing the replacement result in a separate variable. It makes you script more readable.

    3) Why are you passing the control instance to the setValue function instead of the value attribute (e.g. value1.value)?

    i just want to delete "-" character if user entry

    how can i perform it by using javascript?


    Crm Software Specialist

    Wednesday, September 26, 2012 8:59 AM
  • Here is a modified version of the script function.

    function Validatenumber() {
        var value1 = crmForm.all.new_customernumber;
        var result = value1.value.length;
        if (result != 10) {
            alert("Invalid customer number!");
            for (i = 0; i < result; i++) {
                value1.value = value1.value.replace("-", "");
    
            }
    
            Xrm.Page.getAttribute("new_customernumber").setValue(value1.value);
        }
    }

    I have also noticed replace does not automatically replace all occurrences. I thought it did. But it explains the iteration.

    You can also rewrite the iteration using a pattern match and replace like so.

    function Validatenumber() {
        var value1 = crmForm.all.new_customernumber;
        var result = value1.value.length;
        if (result != 10) {
            alert("Invalid customer number!");
                value1.value = value1.value.replace(/-/ig,"");
    
    
            Xrm.Page.getAttribute("new_customernumber").setValue(value1.value);
        }
    }

    • Edited by Oscar Wahlen Wednesday, September 26, 2012 9:48 AM
    • Proposed as answer by Oscar Wahlen Wednesday, September 26, 2012 12:33 PM
    • Marked as answer by Ahmet Çankaya Wednesday, September 26, 2012 1:43 PM
    Wednesday, September 26, 2012 9:40 AM
  • Hi ,

    Please try below code.

    -Anand Wani

    function Validatenumber(){

        var value1 = Xrm.Page.getAttribute("new_customernumber").getValue();
        var result = value1.length;
        if (result != 10) {
            alert("Invalid customer number!");
            value1 = value1.replace("-", "");
            Xrm.Page.getAttribute("new_customernumber").setValue(value1);
        }
    }

    Wednesday, September 26, 2012 12:26 PM