Hi,

Why this code doesn't work? All I want to do is removing all the white spaces.


JavaScript

function validateForm(){
var postcode1 = (document.form1.textPostcode1.value).replace(/^\s*|\s*$/g, "");

if (postcode1 == "") {
alert ("ERROR");
} else {
alert ("CORRECT");
}
}

HTML

<form name="form1" action="save.php" method="POST" onSubmit="return validateForm()">
POSTCODE: <input type="text" name="textPostcode1" size="5" maxlength="4" />
<input type="submit" name="submitButton" value="Submit">
</form>

Thanks

Recommended Answers

All 2 Replies

You forgot the grouping parens

somestring.replace(/(^\s*|\s*$)/g, "")

solved. thanks chanwn

var postcode1 = (document.form1.textPostcode1.value).replace(/ /gi, "");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.