You are viewing limited content. For full access, please sign in.

Question

Question

No space in field

asked on February 28, 2017

Hi,

I got 2 questions:

1. Is it possible to restrict user from entering space in a field. I tried Regular Expression for validation with \w*, but the problem is then the user can't give any special character also...

2. I tired text-transform:capitalize & it works fine if the user is writing everything in lowercase, however if the Caps Lock is on, then it has no impact.

 

Any help is highly appreciated.

 

Regards,

Sahil

0 0

Answer

SELECTED ANSWER
replied on March 2, 2017

For your use case:

 


$(document).ready(function() {

 $('#q1').change(function() {
   var text = $('#q1 input').val();
   $('#q1 input').val(text.replace(/^.|\s.|-./g, function (txt) { return txt.toUpperCase(); }));
 }); 
   
});

1 0

Replies

replied on February 28, 2017 Show version history

1. Try something like this 

^\S*$

Or this

^\s*\S+\s*$
0 0
replied on February 28, 2017

Thanks Blake,

However it only validates if the space is there, I want it other way around... No space should be allowed 

0 0
replied on February 28, 2017

Try [^\s]*?

1 0
replied on February 28, 2017

This seems to work: ^\S+$

0 0
replied on February 28, 2017

Andrew, that works too, Thanks.

Any help on Point 2?

0 0
replied on February 28, 2017 Show version history

You will need to add some javascript for this.
This does a bit more than what you are asking for and might be better written but this should work.   You will need to change the #q9 identifier to your form of course.
( and remove your text-transform)

function Capitilize(s) {
  return s.toLowerCase().replace( /\b./g, function(a){ return a.toUpperCase(); } );
};
$(document).ready(function() {
 $('#q9').change(function() {
    $('#q9 input').val( Capitilize($('#q9 input').val()));
 }); 
  
});

 

1 0
replied on February 28, 2017 Show version history

Andrew,

The function works Great! Thanks. But it only works on the First row of the table & not if I add another? Even if I give Class to the respective field...

 

However, in the first question I have an issue with syntax:

The syntax does not allow any space, this isn't working if the user has 2 First Names....

what would be the syntax to avoid space at the end?

 

Regards,

Sahil

0 0
replied on March 1, 2017

So i added this: 

^[^\s]+(\s+[^\s]+)*$

it works fine as it allows spaces between the names.

0 0
replied on March 1, 2017

Sorry, but I came across another hurdle.

 

If I use any special character (German) like ä,ö,ü,è,é,à

the script leaves it in lower case if it's in the first place & makes the second character upper case, if the special character is in second place, then the script capitalises the special character & also the next character to it.

If the character is written in Caps, then script puts it in lower case & keeps the whole string lower...

0 0
replied on March 1, 2017

A little progress:

function toTitleCase(str) {
            return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
        }

This one at least stops Capitalising the special character & also the one after that.

The only issue is that if the character is in the First place then it makes the next character Uppercase....

i got this from : https://forums.asp.net/t/1988447.aspx?Capitalization+Problem+if+words+contain+German+umlauts+

0 0
replied on March 1, 2017

Getting closer....

function toTitleCase(str) {
            return str.replace(/^.|\s\S./g, function (txt) { return txt.toUpperCase(); });
        }

The issue is if the string has space it capitalises 2 character after space, I guess it should be a small glitch, but my knowledge is very limited....

0 0
replied on March 1, 2017 Show version history

& this is what does the job, only issue is if there is "-" between the names, then this fails....

 

Please help

 

function toTitleCase(str) {
            return str.replace(/^.|\s./g, function (txt) { return txt.toUpperCase(); });
        }

 

0 0
replied on March 2, 2017

Try this:

 

$(document).ready(function() {

 $('#q1').change(function() {
   var text = $('#q1 input').val();
   $('#q1 input').val( text.toLocaleUpperCase());
 }); 
   
});
 

I tested on Chrome and it worked fine using German or special chars or a combination of all cases.

0 0
replied on March 2, 2017

Thanks Pragya,

But it makes the whole text to Uppercase, I want only the first character to be capitalised...

0 0
replied on March 2, 2017

One of the ways of doing it can be:

$(document).ready(function() {

 $('#q1').change(function() {
   var text = $('#q1 input').val();
   $('#q1 input').val( text.substr(0,1).toLocaleUpperCase()+ text.substr(1).toLocaleLowerCase());
 }); 
   
});
 

If you don't want to convert the tailing chars to lowercase you can use:

$('#q1 input').val( text.substr(0,1).toLocaleUpperCase()+ text.substr(1));

And of course if the first char is not an alphabet, it wont capitalize.

0 0
SELECTED ANSWER
replied on March 2, 2017

For your use case:

 


$(document).ready(function() {

 $('#q1').change(function() {
   var text = $('#q1 input').val();
   $('#q1 input').val(text.replace(/^.|\s.|-./g, function (txt) { return txt.toUpperCase(); }));
 }); 
   
});

1 0
replied on March 2, 2017

Awesome!!!

Thanks Pragya!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.