2011-03-03 2 views
5

हम किसी विशिष्ट टेक्स्ट फ़ील्ड में गैर-असीसी वर्णों के उपयोग को प्रतिबंधित करने के लिए जावा स्क्रिप्ट का उपयोग कैसे कर सकते हैं ..? अग्रिम धन्यवाद ...गैर-असीसी अक्षरों का पता लगाने के लिए जावा स्क्रिप्ट नियमित अभिव्यक्ति

+0

क्या आप उन्हें हटा या बदलना चाहते हैं? – drudge

+2

डुप: http://stackoverflow.com/questions/3465874/javascript-regex-to-reject-non-ascii-us-characters (मैं वोटों से बाहर हूं) –

+0

@jnpcl बस उपयोगकर्ता को एक अलर्ट करेगा। ..... उन्हें हटाने का एक अच्छा विकल्प भी है – sasidhar

उत्तर

16

ASCII, 000-177 (अष्टाधारी) की सीमा में पात्रों के रूप में परिभाषित किया गया है इसलिए

function containsAllAscii(str) { 
    return /^[\000-\177]*$/.test(str) ; 
} 

http://jsfiddle.net/V5e4B/1/

आप शायद गैर स्वीकार करने के लिए नहीं करना चाहते हैं प्रिंटिंग वर्ण \000-\037, हो सकता है कि आपका रेगेक्स /\040-\0176/

+2

यदि आप केवल इतना बाहर चाहते बूलियन नहीं है, आप का उपयोग करना चाहिए '.test()' 'बल्कि .exec से()' - यह बजाय एक मैच वस्तु जो तब बूलियन के लिए डाली जानी चाहिए निर्माण की तुलना में, सीधे एक बूलियन पैदा करता है। –

+0

धन्यवाद बेन, मैं भी सही विधि को खोजने के लिए आलसी था। आपके सुझाव के अनुसार फिक्स्ड –

1

मैं इस पृष्ठ पर एक सीएमएस सिस्टम में दोस्ताना यूआरएल के रूप में उपयोग करने के लिए एक स्ट्रिंग को स्वच्छ करने के लिए एक समारोह की तलाश करने की कोशिश कर रहा हूं। सीएमएस बहुभाषी है लेकिन मैं यूआरएल में गैर-एसीआई अक्षरों को दिखाना चाहता हूं। तो श्रेणियों का उपयोग करने के बजाय, मैंने बस (उपरोक्त समाधान के आधार पर) का उपयोग किया:

function verify_url(txt){ 
    var str=txt.replace(/^\s*|\s*$/g,""); // remove spaces 
    if (str == '') { 
     alert("Please enter a URL for this page."); 
     document.Form1.url.focus(); 
     return false; 
    } 
    found=/^[a-zA-Z0-9._\-]*$/.test(str); // we check for specific characters. If any character does not match these allowed characters, the expression evaluates to false 
    if(!found) { 
     alert("The can only contain letters a thru z, A thru Z, 0 to 9, the dot, the dash and the underscore. No spaces, German specific characters or Chinese characters are allowed. Please remove all punctuation (except for the dot, if you use it), and convert all non complying characters. In German, you may convert umlaut 'o' to 'oe', or in Chinese, you may use the 'pinyin' version of the Chinese characters."); 
     document.Form1.url.focus(); 
    } 
    return found; 
} 
संबंधित मुद्दे