How to check input text before post?

Asked

Viewed 56 times

1

Follows the code:

@using (Html.BeginForm("Update", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data" }))
{
.
.
.
<input type="text" name="lastname">
<input type="submit" class="btn btn-success" value="Criar" />
}

I want to check input using javascript or jquery, and then post.

If input is white or empty, do not post.

If different than white, do post

  • Which element ?

  • 1

    @Virgilionovic, I updated post

  • 1

    Do you use jQuery?

  • 1

    @Virgilionovic yes

2 answers

1


Using at the end of page loading:

$(function(){
  var status = !($("input[name=lastname]").length == 1);
  $("input[type=submit]").attr('disabled', status); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="lastname">
<input type="submit" class="btn btn-success" value="Criar" />


With jQuery and jQuery Validation

Form:

<form name="form1" id="form1">
  <input type="text" name="lastname" class="form-control" id="lastname" >
  <input type="submit" class="btn btn-success" value="Criar" />
</form>

Js:

$(document).ready(function() {
  $('#form1').validate({
        rules: {
            lastname: {
                required: true
            }
        },
        messages: {
            lastname: {
                required:'Digite o último nome'
            }
        }
    });  
});

Online Example


With jQuery and Bootstrap Validation

References:

https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js
https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css
https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.8/validator.min.js
https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js
https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap-theme.css

$(function(){
	$('#form1').validator();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap-theme.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.8/validator.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<form id="form1" data-toggle="validator">
    <div class="form-group">
        <label for="exampleInputEmail1">Nome</label>
        <input type="text" name="name" class="form-control" required />
    </div>
    <div class="form-group">
        <label for="exampleInputEmail1">Último nome</label>
        <input type="text" name="lastname" class="form-control" required />
    </div>
    <button class="btn btn-success btn-block" type="submit">Criar</button>
</form>

Online Example

  • Is there a no deactivation button ? Type Jquery Validation

  • Type, when click update button, check input and show red warning

  • You know what happened your question got confused check input it seemed to me if it exists on the page then it would be if the input has entered value?

  • This, if there is no value typed show red warning as if it were "obligatory field"

  • 1

    With Jquery and Jquery Validation to simulate some example.

  • 1

    Thanks, that’s just what I needed

  • @Matheusmiranda take a look at this https://jsfiddle.net/wgrg3z17/10/

  • Wow, that was beautiful

  • 1

    I’ll add in reply also @Matheusmiranda

  • 1

    Long time, thank you very much for answering me

Show 5 more comments

1

If you are not going to use any more complex validation, you can use required HTML5 Here no Javascript required. This way:

<input type="text" name="lastname" required>
<input type="submit" class="btn btn-success" value="Criar">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.