Hi @Harun Ergün
You can perform judgments by adding the attribute "onsubmit" to the form tag.
Example:
Model:
public class Student
{
public string Name { get; set; }
public int Telephone { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
[HttpPost]
public ActionResult form1(string txtName,string txtTel)
{
ViewBag.Name = txtName;
ViewBag.Tel = txtTel;
return View("Index");
}
}
View:
<form action="form1" method="post" onsubmit="return demo()">
<table>
<tr>
<td>Enter Name: </td>
<td><input type="text" name="txtName" id="txtName" /></td>
</tr>
<tr>
<td>Enter Tel: </td>
<td><input type="number" name="txtTel" id="txtTel" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
<script>
function demo() {
var a = document.getElementById("txtName");
var b = document.getElementById("txtTel");
var txtName = a.value;
var txtTel = b.value;
if (txtName.length < 5 && txtTel.length < 11) {
alert("Error");
return false;
}
}
</script>
In this way, a judgment will be made after the data is submitted. Submissions will only be made if they meet the requirements.
Output:
Best Regards
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.