-1

When i try name box is fill with character it always show that "Name must be in character only.".

Here is Javascript code:

function validate_form() {
  if (!(/^[A-Za-z]+$/).test(document.emp.new_name.value)) {
    alert("Name must be in character only.");
    return false;
  }
  if (!(/^\d{10}$/).test(document.emp.new_number.value)) {
    alert("Enter valid mobile number");
    return false;
  }
  if (!(/^[0-9.]+$/).test(document.emp.new_salary.value)) {
    alert("salary must be numeric");
    return false;
  }
  if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/).test(document.emp.new_email.value)) {
    alert("You have entered an invalid email address!")
    return (false);
  }

  alert ("success");
  return true;
}
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
pawas kr. singh
  • 416
  • 1
  • 4
  • 12
  • Please add a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve/) with all relevant data (markup, input values, output/expected result) which shows the actual problem because the part you've shown works as expected: http://jsbin.com/rovorigamo/1/edit?html,js,output – Andreas Feb 14 '18 at 07:26
  • here is the whole code for validation just check – pawas kr. singh Feb 14 '18 at 07:49

2 Answers2

1

Because initially it does not have any characters, try adding a check to character length

Checkout example

$("#submit").on("click",function(){
 var name = $("#name").val();
  if (name.length>0 && !(/^[A-Za-z]+$/).test(name)) {
      alert("Name must be in character only.");
      return false;
  }
  else{
   alert("ok");
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="name"><input type="submit" id="submit">
Fahadsk
  • 1,099
  • 10
  • 24
0
var name = document.emp.new_name.value;
if (name.value.search(/[^a-zA-Z]+/) === -1) {
    alert("Name must be in character only.");
    return false;
}

Try with this code. Hopefully, this will work.

Mahfuz
  • 1
  • 1