简体   繁体   中英

username validation using javascript by document.getElementbyid()

<form method="POST" name="myform" action="<?php $_SERVER['PHP_SELF']; ?>" onsubmit="return validate();">Firstname
    <input type="text" name="firstname" id="firstname">
    <br/>
    <br/>
</form>

My JavaScript validation is

function validate() {

    var A = document.getElementById("firstname").value;
    if (A !== /^[a-zA-Z]/) {
        alert("enter letters only");
        return false;
    }

I want only alphabet letters to enter and if numbers entered means the alert will be displayed.Can any one help me

you have to properly check, if the Regex matches the string:

function validate () {

   var A = document.getElementById("firstname").value;
   if(!(/^[a-z]+$/i.test(A)))
   {
      alert ("enter letters only");
      return false;
   }
}

Additionally the regexp neded to make sure that there is at least one character entered (that is done by the + modificator) and that the a-zA-Z rule applies from start (^) to end ($)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM