locked
CRM 2013 javascript error RRS feed

  • Question

  • Hi All,

    I am having error in the below sentence is there a way i can replace this sentence with something else

    if (toCheck.match(exp1) && !toCheck.match(exp2))

    My whole code is below

    function test1(toCheck) {
    
        //var exp1 = /^([A-CEGHJ-NOPR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1})([0-9]{2})([0-9]{2})([0-9]{2})([A-Z\s]{1})/i;
    	var exp1 = /^([A-Z]{1}[A-Z]{1})([0-9]{2})([0-9]{2})([0-9]{2})([A-Z\s]{1})/i;
    alert("hi1");
        var exp2 = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/i;
    
     // if (toCheck.match(exp1) && !toCheck.match(exp2))
    
    if (toCheck(exp1) && !toCheck(exp2))
    
    {
    
            var strFormated = toCheck.replace(exp1, "$1 $2 $3 $4 $5");
    
            return strFormated.toUpperCase();
    
        }
        else {
    //        return false;
    
     alert("Error: The NI Number must have the following letter/number combinations:\n\n[A-Z] = e.g. SG\n[0-9] = e.g. 56\n[0-9] = e.g. 01\n[0-9] = e.g. 16\n[A-D] = e.g. B"); 
    
        }
    }
    
    

    Any help much appreciated

    Thanks


    Pradnya07

    Thursday, March 27, 2014 11:27 AM

Answers

  • Hi Simran,

    You're using "test1" instead of "test". The "test" is a JavaScript method to check for Regex Validations.

    Cleaned up the code for you and works fine for me:

    function test1(toCheck) 
    {
    	var exp1 = /^([A-Z]{1}[A-Z]{1})([0-9]{2})([0-9]{2})([0-9]{2})([A-Z\s]{1})/;
    	var exp2 = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/;
    
    	if (exp1.test(toCheck) && !exp2.test(toCheck))
    	{
    		var strFormated = toCheck.replace(exp1, "$1 $2 $3 $4 $5");
    		return strFormated.toUpperCase();
    	}
    	else 
    	{
    		alert("Error: The NI Number must have the following letter/number combinations:\n\n[A-Z] = e.g. SG\n[0-9] = e.g. 56\n[0-9] = e.g. 01\n[0-9] = e.g. 16\n[A-D] = e.g. B"); 
    	}
    }


    Admin QuikView Solution for CRM 2013


    • Edited by Anupam Bishui Thursday, March 27, 2014 12:58 PM
    • Marked as answer by Simran08 Thursday, March 27, 2014 3:29 PM
    Thursday, March 27, 2014 12:57 PM
  • Hi Simran,

    I see "test1" instead of "test" in your code, probably that's the reason?


    Admin QuikView Solution for CRM 2013

    • Marked as answer by Simran08 Thursday, March 27, 2014 3:29 PM
    Thursday, March 27, 2014 2:56 PM

All replies

  • Hi Simran,

    There seems to be some syntax error with your JavaScript code.

    First thing, why is there an "i" after each regex that you've defined? The regex declaration should start and end with /. Second thing, if "toCheck" holds a value, and you're trying to test if it matches the regex, use it like exp2.test(toCheck), it would give you a true or false accordingly.


    Admin QuikView Solution for CRM 2013

    Thursday, March 27, 2014 12:00 PM
  • thanks for your reply but i still get the same errror

    after the below modification

    var exp1 = /^([A-Z]{1}[A-Z]{1})([0-9]{2})([0-9]{2})([0-9]{2})([A-Z\s]{1})/;

     var exp2 = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/;

    if (exp1.test1(toCheck) && !exp2.test1(toCheck))


    Pradnya07



    • Edited by Simran08 Thursday, March 27, 2014 12:46 PM 1
    Thursday, March 27, 2014 12:39 PM
  • Hi Simran,

    You're using "test1" instead of "test". The "test" is a JavaScript method to check for Regex Validations.

    Cleaned up the code for you and works fine for me:

    function test1(toCheck) 
    {
    	var exp1 = /^([A-Z]{1}[A-Z]{1})([0-9]{2})([0-9]{2})([0-9]{2})([A-Z\s]{1})/;
    	var exp2 = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/;
    
    	if (exp1.test(toCheck) && !exp2.test(toCheck))
    	{
    		var strFormated = toCheck.replace(exp1, "$1 $2 $3 $4 $5");
    		return strFormated.toUpperCase();
    	}
    	else 
    	{
    		alert("Error: The NI Number must have the following letter/number combinations:\n\n[A-Z] = e.g. SG\n[0-9] = e.g. 56\n[0-9] = e.g. 01\n[0-9] = e.g. 16\n[A-D] = e.g. B"); 
    	}
    }


    Admin QuikView Solution for CRM 2013


    • Edited by Anupam Bishui Thursday, March 27, 2014 12:58 PM
    • Marked as answer by Simran08 Thursday, March 27, 2014 3:29 PM
    Thursday, March 27, 2014 12:57 PM
  • i ran your code but get the below error


    Pradnya07

    Thursday, March 27, 2014 1:14 PM
  • Hi Simran,

    Do you have any other code, in addition to the one you provided here? If it is, there seems to be some error there.


    Admin QuikView Solution for CRM 2013

    Thursday, March 27, 2014 2:42 PM
  • Added alert to your  below code and after alert("4");
    alert("4a");

    i get the attached error

    alert("Error: The NI Number must have the following letter/number combinations:\n\n[A-Z] = e.g. SG\n[0-9] = e.g. 56\n[0-9] = e.g. 01\n[0-9] = e.g. 16\n[A-D] = e.g. B"); 
    
    
    function test1(toCheck) 
    {
    alert("2"); 
    	var exp1 = /^([A-Z]{1}[A-Z]{1})([0-9]{2})([0-9]{2})([0-9]{2})([A-Z\s]{1})/;
    
    
    alert("3"); 
    	//var exp2 = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/;
    alert("4"); 
    alert("4a"); 
    
    	if (exp1.test1(toCheck) )
    && !exp2.test1(toCheck))
    	{
    alert("5"); 
    		var strFormated = toCheck.replace(exp1, "$1 $2 $3 $4 $5");
    		return strFormated.toUpperCase();
    	}
    	else 
    	{
    		alert("Error: The NI Number must have the following letter/number combinations:\n\n[A-Z] = e.g. SG\n[0-9] = e.g. 56\n[0-9] = e.g. 01\n[0-9] = e.g. 16\n[A-D] = e.g. B"); 
    	}
    }
    


    Pradnya07

    Thursday, March 27, 2014 2:52 PM
  • Hi Simran,

    I see "test1" instead of "test" in your code, probably that's the reason?


    Admin QuikView Solution for CRM 2013

    • Marked as answer by Simran08 Thursday, March 27, 2014 3:29 PM
    Thursday, March 27, 2014 2:56 PM
  • no other code with respect to that foeld


    Pradnya07

    Thursday, March 27, 2014 2:56 PM
  • the function name is test1

    so below expression as test1 in it

    if (exp1.test1(toCheck) )&& !exp2.test1(toCheck))

    I didnt get you?


    Pradnya07

    Thursday, March 27, 2014 2:58 PM
  • Hi Simran,

    The "test" in the line if (exp1.test(toCheck) && !exp2.test(toCheck)) is a JavaScript function for checking for Regex validations. I think you're confusing it with the name of your function. Keep the name of your function as "test1" and just change the "test1" in the above line to "test". Did you get it? More details of test function of JavaScript here.


    Admin QuikView Solution for CRM 2013

    Thursday, March 27, 2014 3:05 PM
  • thanks a load for this

    it works like a charm


    Pradnya07

    Thursday, March 27, 2014 3:30 PM