how can add ? ı try but Im not success

Harun Ergün 260 Reputation points
2023-05-15T12:27:55.52+00:00

Successful when you submit the form, if it is incomplete or if it gets an error, how do I add that it cannot be sent?

and ;

Also, for example, rules such as the name must be at least 5 characters and the phone must be at least 11 digits.




@{
    ViewBag.Title = "ISKO DÜNYASINA HOŞGELDİNİZ!";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="~/Scripts/jquery-3.6.4.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


<br />
<br />
<form action="/FormExample1/Create" method="post" class="was-validated">
    <div class="mb-3">
        <label for="İsimgiris" ıd="UserName" class="form-label">İsim</label>
        <input type="text" name="UserName" required="" class="form-control" placeholder="İsim Giriniz..">
    </div>


    <div class="mb-3">
        <label for="Soyisimgiris" class="form-label">Soyisim</label>
        <input type="text" name="UserSurName" required="" class="form-control" placeholder="Soyisim Giriniz..">

    </div>
    <div class="mb-3">
        <label for="Sehir" class="form-label">Şehir</label>
        <input type="text" name="City" required="" class="form-control" placeholder="Şehir Giriniz..">
    </div>
    <br />
    @*RADİO BUTTON*@
    <div class="form-group">
        <label>Cinsiyet :</label>
        <br />
        <br />
        <div class="form-check">
            <input type="radio" class="form-check-input" id="Gender" required="" name="Gender" value="0" checked>Erkek
            <label class="form-check-label" for="radio1"></label>
        </div>
        <div class="form-check">
            <input type="radio" class="form-check-input" id="Gender" required="" name="Gender" value="1">Kadın
            <label class="form-check-label" for="radio2"></label>
        </div>
    </div>
    <br />
    <br />
    <div class="form-group">
        <label>Aslerlik Durumu :</label>
        <br />
        <br />
        <div class="form-check">
            <input type="radio" class="form-check-input" id="Military" required="" name="Military" value="0" checked>Yapıldı
            <label class="form-check-label" for="radio1"></label>
        </div>
        <div class="form-check">
            <input type="radio" class="form-check-input" id="Military" required="" name="Military" value="1">Tecilli
            <label class="form-check-label" for="radio2"></label>
        </div>
        <div class="form-check">
            <input type="radio" class="form-check-input" id="Military" required="" name="Military" value="2">Muaf
            <label class="form-check-label" for="radio2"></label>
        </div>
    </div>
    <br />
    <br />
    <div class="form-group">
        <label>Evlilik Durumu :</label>
        <br />
        <br />
        <div class="form-check">
            <input type="radio" class="form-check-input" id="Marital" required="" name="Marital" value="0" checked>Evli
            <label class="form-check-label" for="radio1"></label>
        </div>
        <div class="form-check">
            <input type="radio" class="form-check-input" id="Marital" required="" name="Marital" value="1">Bekar
            <label class="form-check-label" for="radio2"></label>
        </div>
    </div>
    <br />
    <br />
    <div class="mb-3">
        <label for="telefon1" class="form-label">Telefon</label>
        <input type="number" name="Phone" required="" class="form-control" placeholder="0(XXX) XXX-XX-XX">

    </div>
    <div class="mb-3">
        <label for="telefon2" class="form-label">Telefon 2</label>
        <input type="number" name="Phone2" required="" class="form-control" placeholder="0(XXX) XXX-XX-XX">

    </div>
    <div class="mb-3">
        <label for="İsimgiris" class="form-label">Email</label>
        <input type="email" name="Email" required="" class="form-control" placeholder="Mail giriniz..">

    </div>

    @*TARİH*@

    <div class="mb-3">
        @Html.Label("Tarih", "Lisans Başlangıç Tarihi", htmlAttributes: new { @class = "control-label col-mb-3" })
        <div class="mb-3">

            <input type="date" class="form-control" id="lisansBaslangicTarih" required="" name="lisansBaslangicTarih" placeholder="Lisans Başlangıç Tarihi Giriniz..">
        </div>
    </div>
    <br />
    <div class="mb-3">
        @Html.Label("Tarih", "Lisans Bitiş Tarihi", htmlAttributes: new { @class = "control-label col-mb-3" })
        <div class="mb-3">

            <input type="date" class="form-control" id="lisansBitisTarih" required="" name="lisansBitisTarih" placeholder="Lisans Bitiş Tarihi Giriniz..">
        </div>
    </div>
    <br />


    <div class="mb-3">
        <label class="form-check-label" for="exampleCheck1">Yabancı Dil </label>
        <div class="dropdown">

            <select id="YabanciDil" name="YabanciDil" required="" class="form-control" placeholder="Yabancı Dil Seçiniz..">
                <option value="" disabled selected hidden>Yabancı Dil</option>
                <option>İngilizce</option>
                <option>Fransızca</option>
                <option>Almanca</option>
            </select>
        </div>
    </div>
    <div class="mb-3">
        <label class="form-check-label" for="exampleCheck1"> Seviye </label>
        <div class="dropdown">

            <select id="YabanciDil2" name="YabanciDil2" required="" class="form-control" placeholder="Yabancı Dil Seviyesi Seçiniz..">
                <option value="" disabled selected hidden>Seviye</option>
                <option>Az</option>
                <option>Orta</option>
                <option>İyi</option>
            </select>
        </div>
    </div>

    <br />
    <br />

    <center>
        <button type="submit" id="btn1" class="btn btn-info">Gönder</button>
    </center>


</form>
<body>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

<script src="sweetalert2.min.js"></script>
<link rel="stylesheet" href="sweetalert2.min.css">

<script>
    $('#btn1').on("click", function () { 

        


    });

</script>





ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,597 questions
0 comments No comments
{count} votes

Accepted answer
  1. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-05-16T07:32:45.6066667+00:00

    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:Label

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,121 Reputation points
    2023-05-15T12:48:58.5433333+00:00

    Your asking about input validation. See the official documentation for how to use validation in MVC.

    Adding Validation

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.