0

I'm trying to use javascript to perform client-side verification of input to the following form. The 'firstname' field should contain only alphabetical characters. How can I write the validation function to implement this behaviour?

form.html

<html>
<head>
<script type="text/javascript" src="mainform.js"></script>
</head>
<body>
<form name="mainform" method="post" onSubmit="return validation();" >
    firstname <input type="text" name="firstname"><br>
    lastname <input type="text" name="lastname"><br>
    username <input type="text" name="username"><br>
    password <input type="password" name="password"><br>
    confirm password<input type="password" name="cpassword"><br>
    email <input type="email" name="email"><br>
    phone no <input type="text" name="phoneno"><br>

    <input type="submit" name="submit">
</body>
</html>

form.js

function validation()
{
    var fname=document.mainform.firstname;
    var letters="/[a-zA-Z]$/";
    if(fname.value.match(letters)){
        return true;
    }  
    else  
    {  
        alert('first name must be alphabets');  
        fname.focus();  
        return false;  
    }
}
4
  • 2
    Please define 'is not working'. What is not working. What is your input and expected output. Commented Dec 9, 2014 at 11:19
  • 2
    I also notice you do not have a closing </form> tag. Commented Dec 9, 2014 at 11:20
  • var letters="/[a-zA-Z]$/"; Your regex doesn't need to be in quotes Commented Dec 9, 2014 at 11:25
  • I have written the validation to take characters from a-z or A-Z. But even when iam giving alphabets, it is showing that 'first name must be alphabets' Commented Dec 9, 2014 at 11:25

2 Answers 2

1
function validation()
{
    var fname = document.mainform.firstname;

    if(/^[a-zA-Z]+$/.test(fname.value)) {
        return true;
    } else {
        alert('first name must be alphabets');  
        fname.focus();  
        return false;  
    }
}

Do not forget to make a check on the server side too for security.

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

2 Comments

thank u so much. its working now but i dont want to allow alphabets in phone number field. i pasted the same code for phone number filed. its not working for phone number. var phone = document.mainform.phoneno; if(/^[0-9]+$/.test(phone.value)) { return true; } else { alert('phone'); phone.focus(); return false; }
This should work. I think this is because you put the code at the wrong place. Where did you put that code?
0
<!doctype html>
<html lang="en">  
<head>
  <meta charset="utf-8">
  <title>Hide</title>
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
  <style type="text/css">
  label input[type="button"] 
{

background: #E17698;
}
  </style>
  <script>
 $(document).delegate("#submit","click",function(e)
 {
   var name = $("#fname").val();
   var hasError = false;
  var searchReg = /^[a-zA-Z ]+$/;
 if(name == '') {
              $("#txtt").after('<span class="error" style="color:red;">&nbsp;&nbsp;&nbsp;&nbsp;*Fill Name Box</span>');
              hasError = true;
          } else if(!searchReg.test(name)) {
              $("#txtt").after('<span class="error" style="color:red;">&nbsp;&nbsp;&nbsp;&nbsp;**Enter Vaild Name.</span>');
              hasError = true;
          }else if(searchReg.test(name)) {
              $("#txtt").after('<span class="error" style="color:white;">&nbsp;&nbsp;&nbsp;&nbsp;Vaild Name</span>');

          }
          if(hasError == true) {
  return false
}
});

  </script>
</head>
<body>
<form name="mainform" method="post">
firstname <input type="text" name="firstname" id='fname'><label id="txtt"></label><br>
lastname <input type="text" name="lastname"><br>
username <input type="text" name="username"><br>
password <input type="password" name="password"><br>
confirm password<input type="password" name="cpassword"><br>
email <input type="email" name="email"><br>
phone no <input type="text" name="phoneno"><br>

<input type="submit" name="submit" id='submit'>
</form>
</body>
</html>

1 Comment

Thank u Hussy. But I want the validation code in javascript because i am not familiar with jquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.