javascript prevent from submit

2

I have a text box and a button. I just want to check the value of the textbox with javascript and prevent it from displaying if it is null.

Here is aspx:

<asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox>
<asp:Button CssClass="addButtonBlack" ID="ButtonAddDriver" OnClientClick="return IsNull();" OnClick="ButtonAddDriver_Click" runat="server" Text="Ekle" />

And my javascript code:

function IsNull() {
        var success = true;

        var name = document.getElementById('TextBoxName');
        if (name.value == "") {
            name.style.borderColor = '#e52213';
            name.style.border = 'solid';
            success = false;
        }
        if (success)
            return true;
        else
            return false;
        }

The text box is null. How can I prevent this?

    
asked by anonymous 01.04.2016 / 17:34

2 answers

1

You should have something like: OnClientClick = "IsNull"

function isNull(e) {
  if (it is null) e.preventDefault();
  else ... 
}
    
01.04.2016 / 17:40
3

You can use the javascript event.preventDefault () method to prevent the default event from running

Here is a practical example:

$("a").click(function(event){
    event.preventDefault();
});

A little further down, you can check the text returning and perform a validation using regular expression, returning true or false to the condition you set.

It would look like this:

    $("#buscarID").click(function(e){
        var texto = $("#buscar").val();
        if(validaString(texto)){
            // chama algo
        }else{
            e.preventDefault();
        }
    });

 //Aqui ele só irá retornar true, caso o value for um texto de a-z ou numeros.
 function validaString(value){
     var filter = /[a-zA-Z0-9]+/;
     if(filter.test(value)){return true;}
 }
    
01.04.2016 / 20:49