admin管理员组文章数量:1292385
We have one text Field.We know how to restrict special characters.But We need Allow alphabet and Numbers and hyphen(-) only.No need Sepcial characters but except (-) . Give me any idea.
Mycode:
$('#pduration').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (keyCodeEntered == 45) {
// Allow only 1 minus sign ('-')...
if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0))
return false;
else
return true;
}
}
});
If we tried this code it's restrict spectal charecters but it's allow -,/,+ Please guide me only allow number and alphabet and hyphen only
We have one text Field.We know how to restrict special characters.But We need Allow alphabet and Numbers and hyphen(-) only.No need Sepcial characters but except (-) . Give me any idea.
Mycode:
$('#pduration').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (keyCodeEntered == 45) {
// Allow only 1 minus sign ('-')...
if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0))
return false;
else
return true;
}
}
});
If we tried this code it's restrict spectal charecters but it's allow -,/,+ Please guide me only allow number and alphabet and hyphen only
Share Improve this question edited Mar 5, 2015 at 7:51 Pavan Alapati asked Mar 5, 2015 at 7:43 Pavan AlapatiPavan Alapati 2673 gold badges5 silver badges15 bronze badges 4- give me any idea? have you tried solving this. – Prabhu Murthy Commented Mar 5, 2015 at 7:44
- @unikorn thanks for reply we know only restrice special characters we need except hyphen – Pavan Alapati Commented Mar 5, 2015 at 7:46
- please post the code as well – Prabhu Murthy Commented Mar 5, 2015 at 7:48
- @unikorn Please guide me we add code – Pavan Alapati Commented Mar 5, 2015 at 7:52
3 Answers
Reset to default 2replace this section:
if (keyCodeEntered == 45) {
// Allow only 1 minus sign ('-')...
if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0))
return false;
{
else
return true;
}
with this:
// keys a-z,0-9 numpad keys 0-9 minus sign backspace
if ( ( key >= 48 && key <= 90 ) || ( key >= 96 && key <= 105 ) || key == 109 || key==8)
{
//return true;
}
else
{
//return false
}
})
It's quite easy to do with regex pattern matching.
For JavaScript I remend https://regex101./, and for regex in general i remend Rubular for testing and learning.
A Regex pattern consists looks like this:
/pattern/flags
**First, declare a regex pattern*
/<regex here>/
In order to capture only certain types of characters, we'll use character classes.
/[<char class here]/
Then use this class to match first lowercase letter, first uppercase letter, first number or first "-" character.
/[a-zA-Z0-9-]/
This will only catch the first character
Since we want all matching characters, we add the flag g
for global,
which will return all the characters that match. A final pattern for getting all legal flags loosk like this:
/[a-zA-Z0-9-]/g
That's it for the pattern.
In order to check if something contains illegal characters, like you asked, you can do something like this (both examples work):
function verifyIllegalCharacters (inputString)
{
// Copy the results from replace to new string
// It now holds the original string, minus all legal characters.
// Since they were overwritten by "".
var newStr = inputString.replace(/[a-zA-Z0-9-]/g, "");
// If length is 0, all legal characters were removed,
// and no illegal characters remain.
return (newStr.length == 0);
}
function verifyIllegalCharacters (inputString)
{
// Same, but here we instead check for characters
// NOT matching the pattern. Above we capture all legal chars,
// here we capture all illegal chars by adding a ^ inside the class,
// And overwrite them with "".
var newStr = inputString.replace(/[^a-zA-Z0-9-]/g, "");
// If the lengths aren't equal, something was removed
// If something was removed, the string contained illegal chars.
// Returns true if no illegal chars, else false.
return (newStr.length == inputString.length);
}
I have used this code and Alhamd ul Lillah it is working 100%.
<script type="text/javascript">
/* 48-57 - (0-9) NUMBERS
65-90 - (A-Z)
97-122 - (a-z)
8 - (BACKSPACE)
32 - (SPACE)
45 - '-' (MINUS, HYPHEN, DASH)
*/ // NOT ALLOW SPECIAL
function blockSpecialKeys(e) {
var Keys = e.keyCode;
return (
( Keys >= 65 && k <= 90 ) || // (A-Z)
( Keys >= 97 && k <= 122 ) || // (a-z)
( Keys == 8 ) || // (BACKSPACE)
( Keys == 32 ) || // (SPACE)
( Keys == 45 ) // '-' (MINUS, HYPHEN, DASH)
);
} // END OF blockSpecialKeys FUNCTION
</script>
<input type="text" ... remaining coding ... onKeyPress="return blockSpecialKeys(event);">
I hope you also will get it beneficial!
本文标签: javascripthow to restrict special characters and (*) onlyStack Overflow
版权声明:本文标题:javascript - how to restrict special characters and ( ,*,+) only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741555529a2385132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论