验证输入,不接受第一个和最后一个空格

I would like to put a validation on input, Do not accept spaces from first and last. It accept if there are spaces from the middle.

Example: " Jordan" dont accept "Emily " dont accept "Michael Jackson" accept.

Help me guys. Thank you.

There are couple of native PHP functions that trims the strings from any character and the white space as default:

trim() — Strip whitespace (or other characters) from the beginning and end of a string
ltrim() - Strip whitespace (or other characters) from the beginning of a string
rtrim() - Strip whitespace (or other characters) from the end of a string

So in your case it would be enough to use:

$name = trim($name);

So you can accept all strings and just trim them on serverside before usage.

See it live: https://eval.in/931367

<?php
var_dump(trim(' Jordan'));
var_dump(trim('Emily '));
var_dump(trim('Michael Jackson'));

returns

string(6) "Jordan"
string(5) "Emily"
string(15) "Michael Jackson"

Best way of validating page is using jquery validation plugin

jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
    Email: {
        required: {
            depends:function(){
                $(this).val($.trim($(this).val()));
                return true;
            }
        },
        email: true
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.js"></script>
<form id="myform">
<label for="field">Required: </label>
<input type="text" class="left" id="field" name="Email">
<br/>
<input type="submit" value="Validate!">
</form>

</div>