Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  47] [ 1]  / answers: 1 / hits: 18881  / 11 Years ago, thu, may 30, 2013, 12:00:00

I have a dynamic string like this which may look like:



69.43.202.97                 OR               ngs.pradhi.com


I want to validate these string contains only numbers, english alphabets and . . I want to validate this in front end using java script. I thought of using regular expression like this:



function validatePath() {
var path = document.getElementById('server_path').value;
path.match([a-z][0-9]) //or something like this
}


If the path is invalid despite of displaying the alert box I just want to show the error below the text field as soon as the user fills the server path. How can I do that?



My full javascript function looks like this:



function validatePath() {
var path = document.getElementById('server_path').value;
if (path.search(:) == -1){
alert(Invalid server path);
}
else{
var host_name = path.split(:)[0]
if host_name.match(^[a-zA-Z0-9.]*$)) {}

}


}


More From » validation

 Answers
4

try path.match(^[a-zA-Z0-9.]*$)



EDIT:
var regex = new RegExp(^[a-zA-Z0-9.]*$);
if(regex.test(host_name)){}


[#77923] Wednesday, May 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gonzaloulyssess

Total Points: 225
Total Questions: 114
Total Answers: 112

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;