Hi there,
I am working on form validation, i want to validate "Name" input field value . I want that in "Name" input field no number can be input.

Is there any function which search number/s from string ?? i.e if user inputs any number in that field with string i can display error message(Name can't be alpha numeric).

I will highly appreciate if anyone solve that problem ...

Not sure about that.One way maybe is to loop individual letters in the string and check whether its numeric or not.

To: @developer

Can u plz tell me how it is possible with loop ??

Hi new_developer,

I think a better way may be is to use regular expression to check.Actually the loop thing ain't look good :).
You could use this simple regEx.Please check it. Seems to be working fine.

<script type="text/javascript">
  var myString= "ggekr6k";
  var regEx = new RegExp("[0-9]");
  //Variable 'hasNumber' will be true if the string contains number or false if it doesn't.
  var hasNumber = regEx.test(myString);
</script>

Hi new_developer,

I think a better way may be is to use regular expression to check.Actually the loop thing ain't look good :).
You could use this simple regEx.Please check it. Seems to be working fine.

<script type="text/javascript">
  var myString= "ggekr6k";
  var regEx = new RegExp("[0-9]");
  //Variable 'hasNumber' will be true if the string contains number or false if it doesn't.
  var hasNumber = regEx.test(myString);
</script>

Hi,
I checked that code but I it still not working.

I checked that code but I it still not working.

How you tested it.
I just copy pasted the code and alerts hasNumber and it is working perfectly without any change.

@new_developer
Use regular expression to check.

However, do you want to check that name has no number presented only? Are you sure about that? I will give you the solution for it first, and then I will tell you why you should not do it...

var str1 = "mystring"
var str2 = "my2string"
var str3 = "  "
// will display 'OK' message box
if (str1.match(/\d+/)) { alert("Contain number!") }
else { alert("OK") }

// will display 'Contain number!' message box
if (str2.match(/\d+/)) { alert("Contain number!") }
else { alert("OK") }

// will display 'OK' message box
if (str3.match(/\d+/)) { alert("Contain number!") }
else { alert("OK") }

Now, you should actually think what a name would only contain instead of what should not be in it. How about 'my.name.la' is a name? Or 'me-OK_na' is also a name? How about 'me and you name' is a name too? I could even add 'oh\nthis is\ta name' as well to make it pass your validation if you check for number only.

What you actually should check for is that the name contains only valid characters.

var str1 = "mystring"
var str2 = "my2string"
var str3 = "  "
var str4 = "my string-again."
var str5 = "my\nstring"

// allow alphabet A-Z, a-z, and dash in a name
// display "OK"
if (str1.match(/^[a-z][a-z -]+[a-z]$/i)) { alert("OK") }
else { alert("Contain invalid character!") }

// allow alphabet A-Z, a-z, and dash in a name
// display "Contain invalid character!"
if (str2.match(/^[a-z][a-z -]+[a-z]$/i)) { alert("OK") }
else { alert("Contain invalid character!") }

// allow alphabet A-Z, a-z, and dash in a name
// display "Contain invalid character!"
if (str3.match(/^[a-z][a-z -]+[a-z]$/i)) { alert("OK") }
else { alert("Contain invalid character!") }

// allow alphabet A-Z, a-z, and dash in a name
// display "OK"
if (str4.match(/^[a-z][a-z -]+[a-z]$/i)) { alert("OK") }
else { alert("Contain invalid character!") }

// allow alphabet A-Z, a-z, and dash in a name
// display "Contain invalid character!"
if (str5.match(/^[a-z][a-z -]+[a-z]$/i)) { alert("OK") }
else { alert("Contain invalid character!") }

You may set the allow maximum number of dash & space inside the name if you want to strict it even more. This should at least gives more sense to a name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.