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

Announcements

No record found.

News and Announcements icon
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,264 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans