You must Sign In to post a response.
  • Category: ASP.Net MVC

    Call text from resource file in MVC through javascript

    Hello.
    I am new to MVC.
    Currently I am working on Login form where 2 textbox and 1 button is there.
    On click on button, I check textbox fields empty or not through javascript.
    I have one label below the text box where I display text, "Please enter the user name".
    This validation message I want from resource file.
    How to do that.
  • #768141
    java script code

    function ValidateuserId() {
    var userName = $("#userNametextBox ID").val();
    if (userName == "") {
    alert("Please enter the user .");
    $("#userNametextBox ").focus();
    return false;
    }
    html
    <button type="Submit" onclick=" return ValidateuserId ">


    Anthoter method u can go Data Annonations.


    Thanks & Regards

  • #768142
    Hi,

    We can easily perform this in MVC. But we call that as Jquery function while using MVC.
    1. Creating the Page with 2 textbox and 1 Button and giving ID for the button.



    @Html.LabelFor(m => m.UserName)
    @Html.TextBoxFor(m => m.UserName, new { @class="required", ID="Txtbox1"})


    @Html.LabelFor(m => m.Password)
    @Html.PasswordFor(m => m.Password, new { @class = "required", ID="Txtbox2" })


    <input type="submit" value="Submit" ID="btnLogin"/>


    2. Calling Jquery function while clicking the button

    $(document).ready(function () {
    $("#btnLogin").click({

    If( Txtbox1.val==0)
    {
    alert("Please enter the user name");
    //Or if you have label for that then Label.value = "Please enter the user name"
    }

    })
    }
    )


    3. You have to add the newly created js file into the project.

    <head>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <script src="@Url.Content("~/Scripts/YourJSFile.js")" type="text/javascript"></script>
    </head>

    Thanks,
    Mani

  • #768146
    Thanks guys for response.
    But my query was, how to get that message from local resource file.?

  • #768148
    Resource files as we all know are xml files with .resx extension and have a dedicated designer to manipulate them in Visual Studio.
    you need to read your messages from resource file right ? to accomplish your task you need to first Add Resource Strings, in simple words you need to define it
    if you already add string in resource then check out below link to know how to read messages from resource file
    http://www.devcurry.com/2013/02/aspnet-mvc-using-resource-files-to.html
    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768151
    Thanks for this response.
    This is working properly.
    Can you help me on below thing.

    I have 1 textbox and 1 button.
    On button click, I am checking textbox is empty or not. If empty, assign text, "Please enter the user name" to label and set its visibility to true through javascript.
    This above error message, I want to get it throgh javascript. How to do it.
    Sometimes, I need to display message like, "Invalid username or password" which will come through ViewBag.
    How, can I get this message also through javascript.
    Or there is another way to to it.


  • Sign In to post your comments