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)

Javascript Validation Problem

(0) ShareShare
ReportReport
Posted on by

I will write a javascript code for name filed should not blank.But error message shown but record is saved.so please check below my  code and give suggest where i will be do wrong?

function ValidateNamefield()
{
var name=Xrm.Page.getAttribute("sbl_branch_name").getValue();
if(name==null)
{
alert("Name filed should notbe blank");

}

else
{
Xrm.Page.getAttribute("sbl_branch_name").setValue(name);
}
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    You can simply do this by making field required without JS .

    Or you can create business rules to make field required.

    Try with this if you still want to do using JS. Don't forget to pass execution context in the form onsave event.

    function ValidateNamefield(executionContext) {  

       var name = Xrm.Page.getAttribute("sbl_branch_name").getValue();

       if (name == null &&  name ==  "") {

           alert("Name filed should notbe blank");

           executionContext.getEventArgs().preventDefault();

       }

    }

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    besides making the field required, you also can use setNotification() . So that user cannot save it without resolve the error.

    http://himbap.com/blog/?p=2634

    Last, you can easily achieve it using Business rule if your condition is that simple.

  • Suggested answer
    Sreevalli Profile Picture
    3,256 on at

    Hi,

    Set the field mandatory onsave the CRM will validate and do the job for you :)

    function ValidateNamefield()

    {

    var name=Xrm.Page.getAttribute("sbl_branch_name").getValue();

    if(name==null)

    {

    Xrm.Page.getAttribute("new_myfield").setRequiredLevel("required");

    }

    else

    {

    Xrm.Page.getAttribute("sbl_branch_name").setValue(name);

    }

    }

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