please give me the code for checking the company name entered in textbox using javascript .Only sentence case to be allowed.Only abbreviations (without a full name) should not be allowed.(eg DPS) Abbreviations if any should be allowed only at the end of the name within ().eg Delhi Public School(DPS)

Recommended Answers

And how would you distinguish that "Dps" is not a company name? Or it is?

Jump to Post

OK, you can try to use regular expression to check whether the input string is valid. Below is my attempt to check the string using regex...

// javascript
function isCompanyName(str) {
  str = str.replace(/^\s+|\s+$/g, "")  // remove leading/tailing white spaces
  if (str.match(/^([a-z_]|\W|[A-Z][A-Z]+)/) ||  // start with lower …
Jump to Post

All 5 Replies

And how would you distinguish that "Dps" is not a company name? Or it is?

It is a company name..only thing is user cannot simply enter abbrevation

OK, you can try to use regular expression to check whether the input string is valid. Below is my attempt to check the string using regex...

// javascript
function isCompanyName(str) {
  str = str.replace(/^\s+|\s+$/g, "")  // remove leading/tailing white spaces
  if (str.match(/^([a-z_]|\W|[A-Z][A-Z]+)/) ||  // start with lower case, non word char, or 2 capital letters
      str.match(/\s+[a-z]\w*/) ||  // a word contains all lower cases
      str.match(/\s+[A-Z][A-Z]+/)) {    // a word contains at least 2 capital letters
    alert("'"+str+"' is not a company name!")
  }
}

// HTML element
<input name="whatever" id="whatever" onchange="isCompanyName(this.value)">

Could you suggest a link for me to study regex from scratch?

You may try this one. It is quite good from what I think.

Be a part of the DaniWeb community

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