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?

5
  • What does not work exactly? What is the input that fails? regexper.com/#%2F%5E%5BA-Za-z%5D*%2F Commented Jan 5, 2016 at 12:18
  • 2
    I suspect you missed the $ to ensure a full string check: /^[A-Za-z]*$/. Commented Jan 5, 2016 at 12:18
  • 1
    If adding $ is not yielding expected behavior, please provide a full fiddle to help you better. Commented Jan 5, 2016 at 12:29
  • BTW, since you appear to be validating a name, this article on "Falsehoods Programmers Believe About Names", may be instructive... kalzumeus.com/2010/06/17/… Commented Jan 5, 2016 at 12:31
  • Oh, thanks! I can't believe I missed the $. Anyway, thanks guys!!! Commented Jan 5, 2016 at 12:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.