Users to register only lower case letters

2019-03-06 07:59发布

问题:

I have this register page , and i would like to have the script register only lower case letters : pretty I do not want it to register :Pretty , PRETTy , PRETTY ... Here is the code , what do i need to add , or do i change something in the DB ?

Thanks so much for the help !

public function addField($field_name){

    if (!array_key_exists($field_name, $this->fields))
    {
        if ($field_name=='username') {
            $field = new field_join_username();
            parent::registerField($field);
        }

        if ($field_name=='email') {
            $field = new field_join_email();
            parent::registerField($field);
        }
    }

    parent::addField($field_name);
}

回答1:

You can use strtolower on the input to convert it to lowercase.



回答2:

u can use regular expression for username in both javascript and php pattern would be

javascript

<script type="text/javascript">
var regex = /^[a-z]$/;
var userinput = jQuery('#username').val();
 if (regex.test(userinput )) {
  alert("VALID username");
 } else {
  alert("INVALID username");
 }
}
</script>

php

$pattern = '/^[a-z]$/';
preg_match($pattern, $userinput, $matches);
print_r($matches);