web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Regular Expression for URL by javascript in CRM 2016

(0) ShareShare
ReportReport
Posted on by 340

Hi All,

I'm trying to apply regular exp. for URL which should accept normal standard 

but it is not happening 

here is the code :

function URLtest() {
//var alertreq = false;
var testres = true;

var URLvali = "/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/";
var name = Xrm.Page.getAttribute("new_websiteurl").getValue();//nameeld where u want to check the special character
for (var i = 0; i < name.length; i++) {
if (URLvali.indexOf(name.charAt(i)) != -1) {
testres = false;

}
}

if (testres == false) {
Xrm.Page.getControl("new_websiteurl").setNotification("Website formate should be in correct format");

}
else {
Xrm.Page.getControl("new_websiteurl").clearNotification();
}

}

Plz let me know if any changes required coz it is throwing error for correct format also.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Alok Sharma,

    Please try the following code.

    var URLvali = new RegExp("(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?");

    See: http://stackoverflow.com/questions/8188645/javascript-regex-to-match-a-url-in-a-field-of-text

    Hope this helps.

  • Verified answer
    Alok Sharma Profile Picture
    340 on at

    Hi Nithya,

    I'm using above line so it's throwing error like :

    One of the scripts for this record has caused an error. For more details, download the log file.

    TypeError: URLvali.indexOf is not a function at URLtest (kcb2.crm8.dynamics.com/.../new_URL_Validate)

    I'm using this code can you plz check where I'm committing mistake :

    Code is :

    // JavaScript source code

    function URLtest() {

       //var alertreq = false;

       var testres = true;

       //var iChars = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

       var URLvali =new RegExp("(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");

       var name = Xrm.Page.getAttribute("new_websiteurl").getValue();//nameeld where u want to check the special character

       for (var i = 0; i < name.length; i++) {

           if (URLvali.indexOf(name.charAt(i)) !=1) {

               testres = false;

           }

       }

       if (testres == false) {

           Xrm.Page.getControl("new_websiteurl").setNotification("Website format should be in correct format");

           //alertreq = true;

       }

       else {

           Xrm.Page.getControl("new_websiteurl").clearNotification();

           //alertreq = false;

       }

    }

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Alok,

    Instead of index of, you could use the test method.

    Try the following code.

       var URLvali = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
       var name = Xrm.Page.getAttribute("new_websiteurl").getValue();//nameeld where u want to check the special character
       if (URLvali.test(name)){
               testres = true;
       }
    else{
    testres = false;
    } if (testres == false) { Xrm.Page.getControl("new_websiteurl").setNotification("Website format should be in correct format"); //alertreq = true; } else { Xrm.Page.getControl("new_websiteurl").clearNotification(); //alertreq = false; }

    Hope this helps.

  • Alok Sharma Profile Picture
    340 on at

    HI Nithya,

    Now it is accepting it without domain name

    like if I'm writing google

    it is makeing it http://google  no domain which is not correct it should not accept without domain.

    plz help me !!

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    Try the code below.

    var URLvali = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/;

    Hope this helps.

  • Alok Sharma Profile Picture
    340 on at

    Dear Nithya,

    Thanks for your all support.

    You were always helpful and today also. :)

    Regards,

    Alok Sharma

  • Alok Sharma Profile Picture
    340 on at

    Hi Nithya,

    For same code it was working fine means without domain it was not accepting but now it is again giving problem. It is throwing error for acceptable value also.

    no changes in code but don't know why ?

  • Nithya Gopinath Profile Picture
    17,078 on at

    Which regular expression do you use?

  • Alok Sharma Profile Picture
    340 on at

    i was using this regular exp. var URLvali = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/;

    One of my frnd suggested me this : /\w+:\/\/\w+\.\w+/

    this new one is working fine now but for first time it is giving error either it right or wrong.

    when I'm again attempting then only it is working

    I'm using this code :

    // JavaScript source code

    function URLtest() {

       var testres = false;

       var URLvali = /\w+:\/\/\w+\.\w+/;

       var name = Xrm.Page.getAttribute("new_websiteurl").getValue();//nameeld where u want to check the special character

       if (name.match(URLvali)) {

           testres = true;      

       }

       else {

          testres = false;

       }

       if (testres == false) {

           Xrm.Page.getControl("new_websiteurl").setNotification("Website format should be in correct format");

       }

       else {

           Xrm.Page.getControl("new_websiteurl").clearNotification();

       }

    }

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    Try to use test method instead of match method.

     if (URLvali .test(name)) {
           testres = true;      
       }

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans