0

I'm trying to validate a textbox to see if it contains numbers and special characters, but for some reason, my code doesn't seem to work:

<script>
    function validation(){
        var firstname = $("#Fname").val();
        if(firstname.match(/^[A-Za-z]*/)){
            return true;
        }
        else{
            alert("Invalid name!");
            return false;
        }
    };
</script>

And here is where I called the function:

<form method="POST" action="confirmReservation.php" onSubmit="return validation()" name ="frm">
    <div class="col-md-2 form-group">
        <br><input type="text" id = "Fname" name="Fname" class="form-control"  placeholder="First Name" required>
    </div>
</form>

What might be the problem in my code?

Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66

1 Answers1

0

You can do as following using jquery:

var str = $('#Search').val();
if(/^[a-zA-Z0-9- ]*$/.test(str) == false) {
   alert('Your search string contains illegal characters.');
}

Reference from this post

Community
  • 1
  • 1