trim() से String Cleaning
In this page:
var clean = $.trim(string);
trim() क्या है?
$.trim() किसी string की सिर्फ बिल्कुल शुरुआत और अंत से whitespace characters हटाता है, text के बीच में मौजूद spacing को पूरी तरह untouched छोड़ते हुए।
उदाहरण: What is trim()?
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<div id="output"></div>
<script>
$("#runBtn").click(function() {
$("#output").text("[" + $.trim(" hello ") + "]");
});
$("#resetBtn").click(function() {
$("#output").empty();
});
</script>
</body>
</html>
User Input Trim करना
Users अक्सर गलती से अपने input के आसपास extra spaces type कर देते हैं — अपने name से पहले, अपने email address के बाद — खासकर mobile keyboards पर।
उदाहरण: Trim User Input
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="name" value=" Alice ">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
$("#runBtn").click(function() {
$("#output").text(String($.trim($("#name").val())));
});
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
});
</script>
</body>
</html>
Validation से पहले Trim करें
किसी field के खाली होने की जाँच करने से पहले हमेशा input trim करें, क्योंकि सिर्फ spaces वाली एक string अन्यथा एक empty check pass कर जाएगी भले ही वह।
उदाहरण: Trim Before Validation
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="name" value=" ">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
$("#runBtn").click(function() {
if ($.trim($("#name").val()) === "") {
$("#output").text("Name is empty");
}
});
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
});
</script>
</body>
</html>
बीच के Spaces रहते हैं
trim() सिर्फ string की बिल्कुल शुरुआत और अंत में whitespace को touch करता है — बीच में words के बीच के कोई भी spaces या line breaks बिल्कुल वैसे ही रहते हैं।
उदाहरण: Middle Spaces Stay
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<div id="output"></div>
<script>
$("#runBtn").click(function() {
$("#output").text($.trim(" hello world "));
});
$("#resetBtn").click(function() {
$("#output").empty();
});
</script>
</body>
</html>
एक छोटा trim() Project
एक simple, common pattern यह है कि किसी form को validate या submit करने से ठीक पहले हर text input को trim कर दिया जाए, ताकि accidental leading या trailing spaces कभी।
उदाहरण: Small trim() Project
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="email" value=" [email protected] ">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
function init() {
$("#email").on("blur", function() {
$(this).val($.trim($(this).val()));
});
}
init();
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
init();
});
</script>
</body>
</html>
- यह उम्मीद करना कि
$.trim(" a b ")words के बीच के spaces हटा देगा, जबकि यह सिर्फ शुरुआत और अंत हटाता है। $.trim(null)call करना या किसी number पर call करना और यह मान लेना कि यह same value return करेगा, जबकि यहnullयाundefinedके लिए एक empty string return करता है।- नए code में
$.trim()इस्तेमाल करना, जबकि यह jQuery 3.5 से deprecated है और nativeString.prototype.trim()वही काम करता है:" hi ".trim()।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: