Refer the below Code for Your reference. To use Bundles (ScriptBundle) in ASP.Net MVC Razor. Refer below link
Model
Registration Model
namespace UserValidation
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Registration
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
Controllers
HomeController
namespace UserValidation
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Registration registration)
{
return View(registration);
}
}
}
View -> Home
Index.cshtml
@model UserValidation.Registration
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=btnValidate").click(function () {
var username = $("[id*=txtUserName]").val();
var password = $("[id*=txtPassword]").val();
if (username.toString().toUpperCase() == password.toString().toUpperCase()) {
alert("username and password must be diffrent");
return false;
}
if (username != "" && password != "") {
if ($("[id*=txtUserName]").val().indexOf("@@") > 0) {
if ($("[id*=txtUserName]").val().slice(0, $("[id*=txtUserName]").val().indexOf("@@")).toUpperCase() == password.toUpperCase()) {
alert("Password should not contains userName values");
return false;
}
}
}
});
});
</script>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
<tr>
<td>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { id = "txtUserName" })
</td>
</tr>
<tr>
<td>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { id = "txtPassword" })
</td>
</tr>
<tr>
<td>
<input type="submit" value="Validate" id="btnValidate" />
</td>
</tr>
</table>
}
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
</div>
</body>
</html>
Screenshot