ballouta Posted July 23, 2008 Share Posted July 23, 2008 I already have a javascript that validates several fields. the 'country' field is not working properly with the current function that validates in the same time other fields. the problem is that if the user chooses Turkey for example from the list it works. If he chooses a country name of three words or more it stucks (e.g. United Arab Emirates) in my validation.js: if (isProper(frm.country.value) == false) { alert("Please select a country"); return false; then.... function isProper(string) { if (string.search(/^\w+( \w+)?$/) != -1) return true; else return false; if (!string) return false; var iChars = "*|,\":<>[]{}`\';()@&$#%"; for (var i = 0; i < string.length; i++) { if (iChars.indexOf(string.charAt(i)) != -1) return false; } return true; } Please help Quote Link to comment Share on other sites More sharing options...
ballouta Posted July 23, 2008 Author Share Posted July 23, 2008 any help plz Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 23, 2008 Share Posted July 23, 2008 Why do you need JavaScript validation for a select list? The select list should only contain values that are valid to begin with. You would want to include server-side validation, but client-side validation of a select list is not needed unless some values in a select list are only valid based upon other values in the form. For example, if it was a page to apply for a job you might have an option such as: "Are you a current employee of XYZ Company? Yes/No" Then, a new question with a select list such as: "If not a current employee of XYZ company, how did you hear about us? " In that situation it might be of value to ensure they did not select one of the values in the select list unless they answered no to the previous question (Assuming the first value is null). OR ---- You might want to validate that the user actually selected one of the "Values" from the list if you are using a "Please select a value" as the first option. But, this is very easy to validate by ensuring the item selected is not the first value or not a specific value. Also, most of your isProper() function is worthless. The condition willautomatically return true or false and the rest will not be run at all. What EXACTLY are you trying to achieve? Please state in plain words what you want to validate. Such as "The country should be a string with the following characters a-z, A-Z, underscore, etc. There may be spaces, but they may not begin or end the string and they may not appear in succession." Quote Link to comment Share on other sites More sharing options...
ballouta Posted July 24, 2008 Author Share Posted July 24, 2008 thanks mjdamato for explanation actually i am looking to make sure that the customer has selected a country from the list. it doesn't need any validation as you said. it is clear now i think, thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.