I've a regular expression that allows a string to be empty. If it isn't empty the string may contain only letters and spaces. But it may not start or end with a space.
This RegExp should do this job:
^(?! )[a-zA-Z]*[a-zA-Z ]*$
I've tested it here: http://tools.netshiftmedia.com/regexlibrary/
But when I'm implementing this in my js, it doesn't allow a string to be empty.
This is the code
function validatePreposition(name) {
string = string = document.forms['form'][name].value;
pattern = new RegExp('^(?! )[a-zA-Z]*[a-zA-Z ]*$');
checkString(name, pattern, string);
}
function checkString(name, pattern, string) {
if(!pattern.test(string)) {
error[name] = 1;
} else {
error[name] = 0;
}
printErrors();
}
How change my code so that an empty string is allowed?
(?! )for?/^(?! )[a-zA-Z]*[a-zA-Z ]*$/.test("") === true.