Validation to allow space character only when followed by an alphabet

asp.net, javascript, jquery, validation

Solution

try following regex

 var alpha = /^[a-zA-Z-,]+(\s{0,1}[a-zA-Z-, ])*$/

First part forces an alphabetic char, and then allows space.

Problem

I am trying to validate the textbox which has to allow space character only when followed by any alphabetic character. My code fails when only a space character is inserted. What is the mistake in my code. Suggestions pls.. javascript : ``` function validate() { var firstname = document.getElementById("FirstName"); var alpha = /^[a-zA-Z\s-, ]+$/; if (firstname.value == "") { alert('Please enter Name'); return false; } else if (!firstname.value.match(alpha)) { alert('Invalid '); return false; } else { return true; } } ``` view: ``` @Html.TextBoxFor(m => m.FirstName, new { @class = "searchbox" }) <button type="submit" onclick="return validate();">Submit</button> ``` Conditions I applied : Eg: Arun Chawla - condition success Eg: _ - condition fails (should not allow space character alone)

Original source