Symbol validation error because containing of space - javascript

I was trying to validate a string using javascript, I was trying to validate it in a way it couldn't contain any symbol, my code works, but when user inputs any space it will cause an error.
I tried to erase the string but it didn't work.
Here's what I have tried:
function validateSymbol(word)
{
var newword=word.split(' ').join('');
for(var i = 0; i < newword.length; i++)
{
if(word.charCodeAt(i) < 48 ||
newword.charCodeAt(i) > 57 &&
newword.charCodeAt(i) < 65 ||
newword.charCodeAt(i) > 90 &&
newword.charCodeAt(i) < 97 ||
newword.charCodeAt(i) > 122)
{
return false;
}
}
return true;
}

I think you haven't replaced word with newword in first if condition
function validateSymbol(word)
{
var newword=word.split(' ').join('');
for(var i = 0; i < newword.length; i++)
{
if(newword.charCodeAt(i) < 48 ||
newword.charCodeAt(i) > 57 &&
newword.charCodeAt(i) < 65 ||
newword.charCodeAt(i) > 90 &&
newword.charCodeAt(i) < 97 ||
newword.charCodeAt(i) > 122)
{
return false;
}
}
return true;
}

Related

How can I find out if there are unsupported characters in a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I use the following javascript code to detect unsupported characters in a string.
VerifyTextUnicodeCharacters("My_string");
function VerifyTextUnicodeCharacters(text)
{
var notSupportArray = new Array();
for (var i = text.length - 1; i >= 0; i--)
{
var charCode = text.charCodeAt(i);
if ((charCode == 33) || (charCode == 35) || (charCode == 38) || (charCode >= 40 && charCode <= 42) || (charCode == 46) || (charCode >= 48 && charCode <= 57) || (charCode >= 65 && charCode <= 91) || (charCode == 93) || (charCode == 95) || (charCode >= 97 && charCode <= 123) || (charCode == 125))
{
}
else
{
notSupportArray.push(charCode);
}
}
if (notSupportArray.length == 0)
{
return true;
}
else
{
return false;
}
}
How can I do exactly the same thing in C#?
You just consider string like an array of char:
bool VerifyTextUnicodeCharacters(text)
{
for (var i = 0; i < text.Length; i++)
{
var charCode = (int)text[i];
:
:
}
:
:
}
To translate notsupportArray, you could use List, Array or Queue, here i am using Queue: (Queue is FIFO (First In First Out) collection)
Queue<int> notsupportArray = new Queue<int>();
The equivalent of push is Enqueue:
notsupportArray.Enqueue(charCode);
and the number of items in Queue is:
notsupportArray.Count

I want to pass the text input value to the post ajax call. The javascript function to allow only numbers dot and comma

The below first function code is working absolutely fine for number and dot. However, I want to include comma as well.
This value is stored in an array along with other parameters.
I have also tried the below mentioned 2nd code. It accepts the dot,comma and number only however it stores values as "12.345%2C78" instead of "12.345,78"
function isNumberandComma(evt) {
var iKeyCode = (evt.which) ? evt.which : evt.keyCode
if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)){
return false;
}
return true;
}
function isNumberandComma(evt) {
const NUM_0 = 48;
const NUM_9 = 57;
const KEY_COMMA = 188;
const KEY_DELETE = 46;
const iKeyCode = evt.which || evt.keyCode;
if (iKeyCode >= NUM_0 || iKeyCode <= NUM_9 || iKeyCode === KEY_COMMA || iKeyCode === KEY_DELETE){
return true;
}
return false;
}
Replace your OR operator (||) with the AND operator (&&).
iKeyCode has to be > 48 AND < 57 OR == 188 OR == 46.
if (iKeyCode >= 48 && iKeyCode <= 57 || iKeyCode === 188 || iKeyCode === 46) {
return true;
}

javascript validation allow only character and comma with backspace, arrow and delete keys

I need that textbox only allow characters and comma with arrow keys, backspace and delete keys
$(document).ready(function() {
$('#text').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z,]");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
e.preventDefault();
return false;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
<input type="text" id="text" />
</body>
Check this fiddle
$(document).ready(function() {
$('#text1').keypress(function (e) {
var k = e.which;
var ok = k == 127 || k == 8 || k == 9 || k == 13 || k == 37 || k == 38 || k == 39 || k == 40;
ok = ok ||
k >= 65 && k <= 90 || // A-Z
k >= 97 && k <= 122 || // a-z
k == 44 ; // ,
if (!ok){
e.preventDefault();
}
});
});

In Javascript onkeypress Event,Tab button does not fire(In Mozilla Firefox)

I have a textbox, I need to validate my textbox using onkeypress (to restrict special characters). It works fine in Chrome. In Mozilla Firefox it does not fire (Tab Button). Is there any events to add in my code?
My code:
function alpha(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 9); //k=9(keycode for tab)
}
test this code
function alpha(e) {
var code = e.keyCode || e.which;
if(code == 9) { //Tab keycode
//Do something
}
}
This might help you.
function alpha(e){
var k = e.charCode ? e.charCode : e.keyCode;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 9); //k=9(keycode for tab)
}

Onkeypress, javascript is not validating special character

Javascript function to avoid special characters. It is working as expected with alpha & numbers.only problem is, when i need to include -,. (k >= 188 && k <= 190) - that is not working. what am i doing wrong?
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || (k >= 188 && k <= 190)|| k == 8 || k == 32 || (k >= 48 && k <= 57));
This is my latest code,it will support all the browser.
function isAlphaNumeric(evtGet) {
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer" || browser == "Mozilla Firefox") {
var keyGet = evtGet.keyCode;
} else {
var keyGet = evtGet.which; //(window.Event) ? evtGet.which : evtGet.keyCode;
}
alert(keyGet);
if ((keyGet > 47 && keyGet < 58) || (keyGet > 64 && keyGet < 91) || (keyGet > 96 && keyGet < 123) || (keyGet == 9) || (keyGet == 32) || (keyGet == 8) || (keyGet == 0) || (keyGet == 13)||(keyGet==110))
return;
else
if (browser == "Microsoft Internet Explorer" || browser == "Mozilla Firefox")
window.event.returnValue = null;
else
evtGet.preventDefault();
}
put the above code in the header of the jsp
and call from input field like
onKeyPress="return isAlphaNumeric(event,this.value);">
suppose you want to change in the code as per your requirement,there is a alert box please run the code 1st then click on the key board it will show keycode as per your requirement
example-you want to enter (.)in the field it will show you keycode for dot id 47 and you can write (keyGet==47)

Categories